vue3使用v-model
时间: 2023-07-08 13:47:42 浏览: 177
在Vue3中,使用v-model的方式与Vue2有所不同。以下是使用v-model的步骤:
1. 在子组件中,使用props接收父组件传递的value和emit事件。
```javascript
props: {
value: String
},
emits: ['update:value']
```
2. 在子组件中使用v-model绑定value和update事件。
```html
<template>
<input :value="value" @input="$emit('update:value', $event.target.value)">
</template>
```
3. 在父组件中使用v-model绑定子组件的value和update事件。
```html
<template>
<div>
<child-component v-model="message"></child-component>
</div>
</template>
```
注意:在Vue3中使用v-model时需要在子组件中使用$emit手动触发update事件,同时在emits选项中声明update事件。
阅读全文
相关推荐













