在有些情况下,我们可能需要对一个 prop 进行“双向绑定”。不幸的是,真正的双向绑定会带来维护上的问题,因为子组件可以变更父组件,且在父组件和子组件都没有明显的变更来源。
在一个包含 prop 的假设的组件中,我们可以用以下方法表达对其赋新值的意图:
this.$emit('update:isShow', false)
父组件可以监听那个事件并根据需要更新一个本地的数据 property。
<add-pickup
:is-show.sync="showAddPickup"
/>
会被扩展为:
<add-pickup
@update:isShow="val => showAddPickup = val"
/>
vue 修饰符sync的功能是:当一个子组件改变了一个 prop 的值时,这个变化也会同步到父组件中所绑定。