父组件v-model绑定一个值
以下均为子组件
//模式一
model: {
prop: 'listKey',
event: 'change'
},
props: ['dataList', 'listKey'],//允许其他props
data: () => ({
thisListKey: -1//防止本地直接修改props
}),
watch: {
thisListKey() {
this.$emit('change', this.thisListKey);//emit上面写的event
},
listKey() {
this.thisListKey = this.listKey;//外部修改同步到内部
}
}
//模式二
model: {
prop: "selected",
event: "change",
},
props: ["item", "selected"],
computed: {
thisSelected: {
get() {
return this.selected;
},
set(e) {
this.$emit("change", e);
},
},
},