Skip to content

useVModel

类别
导出体积
486 B
上次更改
8 months ago
相关

简化 v-model 绑定的简写方式,将 props + emit 转换为 ref

我们鼓励你使用 Vue 的 defineModel 而不是这个组合式,然而有一些边缘情况,比如使用 TSXdeep: true 选项,defineModel 不支持。

用法

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
}, })

类型声明

显示类型声明
ts
export interface 
UseVModelOptions
<
T
,
Passive
extends boolean = false> {
/** * 当 passive 设置为 `true` 时,它将使用 `watch` 来与 props 和 ref 同步。 * 而不是依赖于 `v-model` 或 `.sync`。 * * @default false */
passive
?:
Passive
/** * 当设置了 eventName 时,它的值将用于覆盖 emit 事件的名称。 * * @default undefined */
eventName
?: string
/** * 尝试检查嵌套对象或数组中属性的更改。 * 仅在将 `passive` 选项设置为 `true` 时应用 * * @default false */
deep
?: boolean
/** * 在未传递值时定义返回 ref 的默认值。 * * @default undefined */
defaultValue
?:
T
/** * 克隆 props。 * 接受一个自定义克隆函数。 * 当设置为 `true` 时,它将使用 `JSON.parse(JSON.stringify(value))` 进行克隆。 * * @default false */
clone
?: boolean |
CloneFn
<
T
>
/** * 在触发 emit 事件之前的钩子,可用于表单验证。 * 如果返回 false,则不会触发 emit 事件。 * * @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
]>>

源码

源码文档

贡献者

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

更新日志

d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)