Skip to content

使用 V 模型

¥useVModels

props v-model 绑定的简写。就像 toRefs(props) 一样,但更改也会触发触发。

¥Shorthand for props v-model binding. Think it like toRefs(props) but changes will also trigger emit.

用法

¥Usage

ts
import { 
useVModels
} from '@vueuse/core'
const
props
=
defineProps
({
foo
: string,
bar
: number,
}) const
emit
=
defineEmits
(['update:foo', 'update:bar'])
const {
foo
,
bar
} =
useVModels
(
props
,
emit
)

选项 API

¥Options API

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