vue复用echarts组件使用this,$refs代替id获取对象

vue yekong 1460℃

外包项目中经常会遇到使用多个echart图表的情况,尤其是数据可视化大屏 使用echarts更多,每次使用echarts组件时,都需要专门修改一次id的话太麻烦了,而且可能会出现遗漏,造成对其他echarts的干扰,那么怎么解决呢?使用id也需要每次都重复写没办法复用找了一个解决办法,id是唯一的,vue可以不用id获取对象,改为this,$refs来进行获取,这样就可以服用echarts组件了。


<div class="echarts my-echarts" ref="echarts" id="echarts"></div>

将id 修改一下


this.charts = echarts.init(document.getElementById(echarts));

使用this.$refs

this.charts = echarts.init(this.$refs.echarts);
喜欢 (0)