当父组件向子组件传递参数时,子组件是不可以直接更改父组件的参数,所以要这么写
//子组件
this.$emit('update:bubble', 1)// 传1每次减1
//父组件
为了方便起见,我们为这种模式提供一个缩写,即 .sync 修饰符:
//子组件
this.$emit('update:bubble', this.bubble - 1)
//父组件
当我们用一个对象同时设置多个 prop 的时候,也可以将这个 .sync 修饰符和 v-bind 配合使用:
//父组件
//子组件
export default {
props: ['bubble', 'a', 'b'],
methods: {
fn(i) {
this.$emit('update:b', this.b - 1)
}
}
}
在没有sync时,我们要实现双向绑定,可能需要在父组件里绑定一个事件和一个值,这样会很麻烦,但是有了sync之后,我们就可以这么简写。