Skip to content

useVModel

Category
Export Size
486 B
Last Changed
7 months ago
Related

Shorthand for v-model binding, props + emit -> ref

We encourage you to use Vue's defineModel over this composable, however there are some edge-cases like using TSX or the deep: true option that defineModel doesn't support.

Usage

ts
import { 
useVModel
} from '@vueuse/core'
const
props
=
defineProps
<{
modelValue
: string
}>() const
emit
=
defineEmits
(['update:modelValue'])
const
data
=
useVModel
(
props
, 'modelValue',
emit
)
js
import { useVModel } from '@vueuse/core'
const props = defineProps()
const emit = defineEmits(['update:modelValue'])
const data = useVModel(props, 'modelValue', emit)

Options API

ts
import { 
useVModel
} from '@vueuse/core'
export default {
setup
(
props
, {
emit
}) {
const
data
=
useVModel
(
props
, 'data',
emit
)
console
.
log
(
data
.
value
) // props.data
data
.
value
= 'foo' // emit('update:data', 'foo')
}, }

Options

Passive Mode

By 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.

ts
const 
data
= useVModel(props, 'modelValue', emit, {
passive
: true })

Deep Watching

When using passive: true, you can enable deep watching for nested object changes:

ts
const 
data
= useVModel(props, 'modelValue', emit, {
passive
: true,
deep
: true,
})

Clone Values

Clone the prop value to avoid mutating the original object. Set to true to use JSON.parse(JSON.stringify()) or provide a custom clone function.

ts
const 
data
= useVModel(props, 'modelValue', emit, {
clone
: true,
// or provide custom clone function // clone: (val) => structuredClone(val), })

Default Value

Provide a default value when the prop is undefined:

ts
const 
data
= useVModel(props, 'modelValue', emit, {
defaultValue
: 'default',
})

Custom Event Name

Override the default update:propName event name:

ts
const 
data
= useVModel(props, 'value', emit, {
eventName
: 'change',
})

Emit Validation

Use shouldEmit to validate before emitting. Return false to prevent the emit:

ts
const 
data
= useVModel(props, 'modelValue', emit, {
shouldEmit
: (
value
) => {
// Only emit if value is valid return
value
.length > 0
}, })

Type Declarations

Show Type Declarations
ts
export interface 
UseVModelOptions
<
T
,
Passive
extends boolean = false> {
/** * When passive is set to `true`, it will use `watch` to sync with props and ref. * Instead of relying on the `v-model` or `.sync` to work. * * @default false */
passive
?:
Passive
/** * When eventName is set, it's value will be used to overwrite the emit event name. * * @default undefined */
eventName
?: string
/** * Attempting to check for changes of properties in a deeply nested object or array. * Apply only when `passive` option is set to `true` * * @default false */
deep
?: boolean
/** * Defining default value for return ref when no value is passed. * * @default undefined */
defaultValue
?:
T
/** * Clone the props. * Accepts a custom clone function. * When setting to `true`, it will use `JSON.parse(JSON.stringify(value))` to clone. * * @default false */
clone
?: boolean |
CloneFn
<
T
>
/** * The hook before triggering the emit event can be used for form validation. * if false is returned, the emit event will not be triggered. * * @default undefined */
shouldEmit
?: (
v
:
T
) => boolean
} /** * Shorthand for v-model binding, props + emit -> ref * * @see https://vueuse.org/useVModel * @param props * @param key (default 'modelValue') * @param emit * @param options * * @__NO_SIDE_EFFECTS__ */ export declare function
useVModel
<
P
extends object,
K
extends keyof
P
,
Name
extends string,
>(
props
:
P
,
key
?:
K
,
emit
?: (
name
:
Name
, ...
args
: any[]) => void,
options
?:
UseVModelOptions
<
P
[
K
], false>,
):
WritableComputedRef
<
P
[
K
]>
export declare function
useVModel
<
P
extends object,
K
extends keyof
P
,
Name
extends string,
>(
props
:
P
,
key
?:
K
,
emit
?: (
name
:
Name
, ...
args
: any[]) => void,
options
?:
UseVModelOptions
<
P
[
K
], true>,
):
Ref
<
UnwrapRef
<
P
[
K
]>>

Source

SourceDocs

Contributors

Anthony Fu
Anthony Fu
Jelf
SerKo
webfansplz
Shinigami
motian
objectisundefined
Alex Liu
白雾三语
Eduardo Wesley
Roman Meshcheryakov
久染
chaii3
Tmk
wheat
sondh0127
Zenthae
Eduardo San Martin Morote
lstoeferle
Alex Kozack
Homyee King
Prabu Rangki
Leonidas Arvanitis
yangger

Changelog

v13.6.0 on
d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.4.0 on
48f4c - fix: clone set to true triggered infinite loop (#3097)

Released under the MIT License.

2 for 1 SALE
Free JavaScript Certification with any Certification purchased
Get Certified
02
hours
:
40
minutes
:
45
seconds
: