1. 在组件上使用v-model
<input v-model="searchText">
等价于
<input
v-bind:value="searchText"
v-on:input="searchText = $event.target.value"
>
实现一个有v-model功能的自定义组件:
<custom-input v-model="searchText"></custom-input>
Vue.component('custom-input', {
props: ['value'],
template: `
<input
v-bind:value="value"
v-on:input="$emit('input', $event.target.value)"
>
`
})
文章介绍了如何在Vue中使用v-model以及其等价的属性绑定和事件监听方式。接着,通过一个示例展示了如何在自定义组件上实现v-model的功能,通过接收valueprop和发射input事件来同步组件内外的数据。
879

被折叠的 条评论
为什么被折叠?



