Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useVModel.md
1---2category: Component3---45# useVModel67Shorthand for v-model binding, props + emit -> ref89> We encourage you to use Vue's [`defineModel`](https://vuejs.org/api/sfc-script-setup.html#definemodel) over this composable, however there are some edge-cases like using `TSX` or the `deep: true` option that `defineModel` doesn't support.1011## Usage1213```ts14import { useVModel } from '@vueuse/core'1516const props = defineProps<{17modelValue: string18}>()19const emit = defineEmits(['update:modelValue'])2021const data = useVModel(props, 'modelValue', emit)22```2324### Options API2526```ts27import { useVModel } from '@vueuse/core'2829export default {30setup(props, { emit }) {31const data = useVModel(props, 'data', emit)3233console.log(data.value) // props.data34data.value = 'foo' // emit('update:data', 'foo')35},36}37```3839## Options4041### Passive Mode4243By default, `useVModel` returns a computed ref. In passive mode, it creates a local ref that syncs with the prop via `watch`, allowing deep reactivity tracking.4445```ts46const data = useVModel(props, 'modelValue', emit, { passive: true })47```4849### Deep Watching5051When using `passive: true`, you can enable deep watching for nested object changes:5253```ts54const data = useVModel(props, 'modelValue', emit, {55passive: true,56deep: true,57})58```5960### Clone Values6162Clone the prop value to avoid mutating the original object. Set to `true` to use `JSON.parse(JSON.stringify())` or provide a custom clone function.6364```ts65const data = useVModel(props, 'modelValue', emit, {66clone: true,67// or provide custom clone function68// clone: (val) => structuredClone(val),69})70```7172### Default Value7374Provide a default value when the prop is undefined:7576```ts77const data = useVModel(props, 'modelValue', emit, {78defaultValue: 'default',79})80```8182### Custom Event Name8384Override the default `update:propName` event name:8586```ts87const data = useVModel(props, 'value', emit, {88eventName: 'change',89})90```9192### Emit Validation9394Use `shouldEmit` to validate before emitting. Return `false` to prevent the emit:9596```ts97const data = useVModel(props, 'modelValue', emit, {98shouldEmit: (value) => {99// Only emit if value is valid100return value.length > 0101},102})103```104105## Type Declarations106107```ts108export interface UseVModelOptions<T, Passive extends boolean = false> {109/**110* When passive is set to `true`, it will use `watch` to sync with props and ref.111* Instead of relying on the `v-model` or `.sync` to work.112*113* @default false114*/115passive?: Passive116/**117* When eventName is set, it's value will be used to overwrite the emit event name.118*119* @default undefined120*/121eventName?: string122/**123* Attempting to check for changes of properties in a deeply nested object or array.124* Apply only when `passive` option is set to `true`125*126* @default false127*/128deep?: boolean129/**130* Defining default value for return ref when no value is passed.131*132* @default undefined133*/134defaultValue?: T135/**136* Clone the props.137* Accepts a custom clone function.138* When setting to `true`, it will use `JSON.parse(JSON.stringify(value))` to clone.139*140* @default false141*/142clone?: boolean | CloneFn<T>143/**144* The hook before triggering the emit event can be used for form validation.145* if false is returned, the emit event will not be triggered.146*147* @default undefined148*/149shouldEmit?: (v: T) => boolean150}151/**152* Shorthand for v-model binding, props + emit -> ref153*154* @see https://vueuse.org/useVModel155* @param props156* @param key (default 'modelValue')157* @param emit158* @param options159*160* @__NO_SIDE_EFFECTS__161*/162export declare function useVModel<163P extends object,164K extends keyof P,165Name extends string,166>(167props: P,168key?: K,169emit?: (name: Name, ...args: any[]) => void,170options?: UseVModelOptions<P[K], false>,171): WritableComputedRef<P[K]>172export declare function useVModel<173P extends object,174K extends keyof P,175Name extends string,176>(177props: P,178key?: K,179emit?: (name: Name, ...args: any[]) => void,180options?: UseVModelOptions<P[K], true>,181): Ref<UnwrapRef<P[K]>>182```183