1 回答
TA贡献1851条经验 获得超3个赞
watch在created之后就开始监听变化了,在mounted之前,props的option应该发生了一次变化,而这时你的chartDom还没渲染好,所以会出现图中的报错。把chart.init操作移至mounted中就行了。还有,不用每次option变动都去执行init,只要执行一下setOption就能重绘了。
另外,你DOM容器变化后才需要resize,比如调整窗口大小后,DOM宽度变小了,那就执行以下resize(这种情况最好配合debounce使用)
还是直接上代码吧……
watch: {
option: function (val) {
if (val) this.renderChart();
}
},
mounted(){
this.chart = echarts.init(this.$refs.myEchart);
window.addEventListener("resize", this.onResize, false);
},
destroyed(){
window.removeEventListener("resize", this.onResize, false);
},
methods: {
onResize(){
if(this.chart){
this.chart.resize();
}
}
renderChart () {
// this.chart = echarts.init(this.$refs.myEchart);
this.chart.setOption(this.option);
}
}
添加回答
举报
