项目中使用的是折线图, 里面的数据是数组的形式.
需求 : 每隔两秒请求一次最新数据, 然后更新到数组.
问题: 每次请求完数据,需调用一次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);
}