多个echart图表添加resize事件


由于我的图表都是循环渲染出来的,一开始是在每个循环当中添加的 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)
}

这样就可以了


 本篇
多个echart图表添加resize事件 多个echart图表添加resize事件
由于我的图表都是循环渲染出来的,一开始是在每个循环当中添加的 resize 事件 const chartDom = document.getElementById('chart' + item.id) this['chartDom' + i
2021-08-19 zjt
下一篇 
linux路径正则表达式匹配 linux路径正则表达式匹配
开发时遇到表单需要校验linux文件路径,要求是开头必须是/,中间不能有空格和特殊符号,结尾可以是文件或是文件夹正确格式 / /文件夹 /文件夹/ /文件夹/process.yml 错误格式 opt /opt// /opt/asd? 表达式
2021-06-15 zjt
  目录