vue3实现v-model
时间: 2023-09-01 18:10:05 浏览: 112
vue3实现v-model原理详解
5星 · 资源好评率100%
在Vue3中,v-model可以用于组件中。当v-model用于组件时,它将展开为以下形式:
```
<CustomInput :modelValue="searchText" @update:modelValue="newValue => searchText = newValue" />
```
这里的`:modelValue`是用来接收父组件传递给子组件的值,而`@update:modelValue`是用来在子组件中更新传递给父组件的值的事件。子组件需要通过`defineProps`来声明接收的属性,通过`defineEmits`来声明触发的事件。在子组件的模板中,可以使用`modelValue`来访问父组件传递的值,并通过触发`update:modelValue`事件来更新该值。这样,就实现了类似于原生表单元素的双向绑定效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [Vue3: v-model](https://blog.csdn.net/Everglow___/article/details/130222058)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [vue3中组件的v-model使用及详解](https://blog.csdn.net/kzj0916/article/details/120477925)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文