主题
使用 V 模型
¥useVModel
v-model 绑定的简写,props+emit->ref
¥Shorthand for v-model binding, props + emit -> ref
我们鼓励你使用 Vue 的
defineModel而不是此可组合项,但是有一些特殊情况,例如使用TSX或defineModel不支持的deep: true选项。¥We encourage you to use Vue's
defineModelover this composable, however there are some edge-cases like usingTSXor thedeep: trueoption thatdefineModeldoesn'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)选项 API
¥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')
},
}