vue项目中
Error in callback for immediate watcher "data": "TypeError: Cannot read property 'clientWidth' of undefined"
@Watch('data',{ immediate: true, deep: true })
onChangeValue(newVal: string, oldVal: string) {
this.getLineData();
this.drawSvg();
}
其中getLineData()方法中操作了dom
const boxDom: any = this.$refs.chartsLineBox;
const boxWidth: number = boxDom.clientWidth;
this.boxHeight = boxDom.clientHeight;
const itemHeight: number = Math.floor(boxWidth / dataCount);
Watch中监听的data是在父组件中通过axios请求到的,在mounted中调用的,不过data有初始值,这种情况应该怎么改?
watch方法执行的时候chartsLineBox还没渲染好吧?不知道你的业务场景是什么,可以考虑把immediate设置为false。总之确保在调用getLineData的时候chartsLineBox这个元素渲染好了。