子组件sg-component.vue代码
父组件(引用子组件)代码
渲染效果
注意!在子组件内部不要用v-model绑定modelValue变量,否则会报错Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "modelValue"
那么如何解决这一“双重双向绑定”报错问题呢?
我们可以在子组件内部引入一个中间变量(子组件内部作用域有效),用$emit()方法来触发来同步更新子组件内部的中间变量变化值,这样就可以有效避开“双重双向绑定”报错问题了。
修改后的组件sg-component.vue代码
这样就不会报错了~!