父子组件传值,如果想要子组件中操作数据,可能出现两个问题
- 父组件传show:
<center-popup :show="show">
子组件 props接收
data(){
return{
show:false,
}
},
props:{
show:{
typle: Boolean,
default: false,
},
此时报错
The data property “show” is already declared as a prop. Use prop default value instead.
就需要把上述data中的show改为isshow
data(){
return{
isshow:false,
}
},
- 子组件操作数据可能会出现问题
[Vue warn]: 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: “show”
解决:
在子组件中的watch监听数据变化,并且在watch中把值赋值给当前子组件data中的值
watch: {
show: function(newVal,oldVal){
this.isshow = newVal; //newVal即是chartData
}
},