使用Echarts报错 Error in created hook: “TypeError: Cannot read property ‘getAttribute‘ of null“

出现该错误的原因是Echarts的图形容器还未生成就对其进行了初始化所造成的
在这里插入图片描述
解决办法 只需要延迟初始化就可以,也就是你的 option 使用 this.$nextTick(function () { }) 延迟执行

optionPie() {
    
    
  this.$nextTick(function () {
    
    
    // 通过id获取获取画布
    const myChart = echarts.init(document.getElementById('pie'));
    //图表数据
    let option = {
    
    
      tooltip: {
    
    
        trigger: 'item',
        formatter: '{a} <br/>{b}: {c} ({d}%)'
      },
      legend: {
    
    
        orient: 'vertical',
        left: 10,
        data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
      },
      series: [
        {
    
    
          name: '访问来源',
          type: 'pie',
          radius: ['50%', '70%'],
          avoidLabelOverlap: false,
          label: {
    
    
            show: false,
            position: 'center'
          },
          emphasis: {
    
    
            label: {
    
    
              show: true,
              fontSize: '30',
              fontWeight: 'bold'
            }
          },
          labelLine: {
    
    
            show: false
          },
          data: [
            {
    
    value: 335, name: '直接访问'},
            {
    
    value: 310, name: '邮件营销'},
            {
    
    value: 234, name: '联盟广告'},
            {
    
    value: 135, name: '视频广告'},
            {
    
    value: 1548, name: '搜索引擎'}
          ]
        }
      ]
    };
    // 使用刚指定的配置项和数据显示图表。画布为 myCharts  true为显示
    if (option && typeof option === "object") {
    
    
      myChart.setOption(option, true);
    }
  })
}

猜你喜欢

转载自blog.csdn.net/weixin_44640323/article/details/108829508