Skip to content

useVModels

类别
导出体积
509 B
上次更改
2 months ago
相关

propsv-model 绑定的简写方式。可以将其视为 toRefs(props),但是对属性的更改也会触发 emit

用法

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
)

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

类型声明

ts
/**
 * 用于 props 的 v-model 绑定的简写。类似于 `toRefs(props)`,但更改也会触发 emit。
 *
 * @see https://vueuse.org/useVModels
 * @param props
 * @param emit
 * @param options
 *
 * @__NO_SIDE_EFFECTS__
 */
export declare function 
useVModels
<
P
extends object,
Name
extends string>(
props
:
P
,
emit
?: (
name
:
Name
, ...
args
: any[]) => void,
options
?:
UseVModelOptions
<any, true>,
):
ToRefs
<
P
>
export declare function
useVModels
<
P
extends object,
Name
extends string>(
props
:
P
,
emit
?: (
name
:
Name
, ...
args
: any[]) => void,
options
?:
UseVModelOptions
<any, false>,
):
ToRefs
<
P
>

源码

源码文档

贡献者

Anthony Fu
一纸忘忧
SerKo
broBinChen
Mikhailov Nikita
Jelf
sondh0127
William T. Kirby
Alex Kozack
ordago
Emīls Gulbis

更新日志

d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
51f01 - fix: type error with passive: true (#3362)