使用echarts报错 Cannot read properties of undefined (reading ‘getInstanceByDom‘) at eval

项目中使用的是折线图, 里面的数据是数组的形式.
需求 : 每隔两秒请求一次最新数据, 然后更新到数组.
问题: 每次请求完数据,需调用一次echarts函数,不然数据更新不上, 但是每次调用就会创建, 因为已经创建了一次dom. 就会导致发出警告;
解决方法: 判断已经存在echarts实例,如果不存在就创建,反之,则不创建.
上代码:

// 在export default外面  
let rateChart; 

// 在echarts函数里面
    drawCharts() {
    
    
	 if (rateChart != null && rateChart != "" && rateChart != undefined) {
    
    
	      rateChart.dispose(); //销毁
	   }
     rateChart = Echarts.init(this.$refs.main);
     // 函数最下面
 option && rateChart.setOption(option);
}

猜你喜欢

转载自blog.csdn.net/weixin_39891473/article/details/126262999