vue 父子传参的时候,在子组件监听父组件传过来的参数,页面也是正常渲染的,控制台一直报 Cannot read properties of undefined (reading '0')
代码:
props: {
pie1value: {
type: Object,
default: () => {},
}
},
watch:{
pie1value:{
handler(newvalue,oldvalue){
if(newvalue) {
this.getCharts(newvalue)
this.measureList = newvalue.measureList
this.sourceId = newvalue.sourceId
this.datakeyc = newvalue.dimensionFilterList[0].colKey
this.datavalues = newvalue.dimensionFilterList[0].colVal
}
},
immediate:true,
deep:true
}
}
打印了newvalue,控制台显示打印了两次
由此可见,是触发了两次,第一次没有值,所以报错
解决:
增加个 if 判断,有值的时候进行赋值
if(newvalue.dimensionFilterList) {
this.datakeyc = newvalue.dimensionFilterList[0].colKey
this.datavalues = newvalue.dimensionFilterList[0].colVal
}