首页 > 其他 > 详细

vue中为多个echart循环添加resize事件

时间:2021-08-19 17:34:19      阅读:81      评论:0      收藏:0      [点我收藏+]

由于我的图表都是循环渲染出来的,一开始是在每个循环当中添加的resize事件

const chartDom = document.getElementById(‘chart‘ + item.id)
this[‘chartDom‘ + item.id] = echarts.init(chartDom)
const myChart = this[‘chartDom‘ + item.id]
......图表渲染
window.addEventListener(‘resize‘, () => {
   myChart.resize()
})

后来发现这样没有办法在beforedestory中清除window上的resize方法,因为要想使用reomveEventListener的话是不能使用匿名函数的,必须像类似这样的写法

window.addEventListener(‘resize‘, this.chartResize)
window.removeEventListener(‘resize‘, this.chartResize)

这样的话还不能向这个函数中传入参数,于是我改为在循环外添加resize方法

async mapGetChartData (val) {
    for (const item of this.chartList) {
      await this.getChartData(item)
    }
    window.addEventListener(‘resize‘, this.chartResize)
}

chartResize () {
    this.chartdomList.map(item => {
      item.resize()
  })
}

beforeDestroy () {
    window.removeEventListener(‘resize‘, this.chartResize)
}

这样就可以了

vue中为多个echart循环添加resize事件

原文:https://www.cnblogs.com/imjtzhang/p/15162288.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!