学无先后,达者为师

网站首页 编程语言 正文

TypeError:cannot read property ‘getAttribute‘ of undefined

作者:前端开心果 更新时间: 2023-07-31 编程语言

报错场景:页面切换路由时报错
报错原因:使用 Vue + echarts,因为 页面中使用了 echarts 图表,在初始化图表后,dom 元素还未被初始化加载出来,导致 echarts 在渲染时候报错
示例代码:

drawPie(){ // 总体告警数 饼图
  // 基于准备好的dom,初始化echarts实例
  let ref = this.$refs.pie
  if (ref && ref !== undefined) {
    let myChart = this.$echarts.init(ref)
    // 绘制图表
    myChart.setOption({
      color: ['#04befe'],
      tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b}: {c}'
      },
      series: [
        {
          type: 'pie',
          radius: ['50%', '65%'],
          avoidLabelOverlap: true,
          label: {
            show: true,
            formatter: ' {c}\n\n{b}',
            fontSize: '16',
            position: 'center'
          },
          emphasis: {
            label: {
              show: true,
              fontSize: '16',
            }
          },
          labelLine: {
            show: false
          },
          data: [
            {value: this.total_num, name: '总体告警数'},
          ],
          hoverOffset: 2
        }
      ]
    })
  }
},

函数中直接调用 echarts 方法即可

原文链接:https://blog.csdn.net/qq_38157825/article/details/109319812

  • 上一篇:没有了
  • 下一篇:没有了
栏目分类
最近更新