Vue eCharts Cannot read property ‘init‘ of undefined 报错问题

本文介绍了在Vue项目中使用ECharts遇到的“init of undefined”错误及其解决方法,包括确保DOM元素尺寸正确、初始化时机选择及使用$nextTick确保DOM更新完成。

在这里插入图片描述
在vue 项目中使用 eCharts ,但是项目一直报 init of undefined ,查阅很多资料,方法不一

1,元素没有设长宽

<template>
  <div class="container">
    <div id="myChart" ref="myChart" style="width: 600px; height: 400px"></div>
  </div>
</template>

2,初始化实例应该写在 mounted

  mounted() {
  let myCharts =  this.$echarts.init(this.$refs.myChart); ;

    let options = {
      title: {
        text: "未来一周气温变化", //图表顶部的标题
        subtext: "纯属虚构", //副标题
      },
      tooltip: {
        //鼠标悬浮框的提示文字
        trigger: "axis",
      },
      legend: {
        data: ["最高气温", "最低气温"],
      },
      xAxis: [
        {
          //x轴坐标数据
          type: "category",
          boundaryGap: false,
          data: ["周一", "周二", "周三", "周四", "周五", "周六", "周日"],
        },
      ],
      yAxis: [
        {
          //y轴坐标数据
          type: "value",
          axisLabel: {
            formatter: "{value} °C",
          },
        },
      ],
      series: [
        //驱动图表生成的数据内容数组,几条折现,数组中就会有几个对应对象,来表示对应的折线
        {
          name: "最高气温",
          type: "line", //pie->饼状图  line->折线图  bar->柱状图
          data: [11, 11, 15, 13, 12, 13, 10],
        },
        {
          name: "最低气温",
          type: "line", //pie->饼状图  line->折线图  bar->柱状图
          data: [1, -2, 2, 5, 3, 2, 0],
        },
      ],
    };
    myCharts.setOption(options);
  }

3,mounted最好加上 $nextTick

  init() {
    console.log(this.$echarts);
    console.log((document.getElementById('myChart')));
    let myCharts =  this.$echarts.init(this.$refs.myChart); ;

    let options = {
      title: {
        text: "未来一周气温变化", //图表顶部的标题
        subtext: "纯属虚构", //副标题
      },
      tooltip: {
        //鼠标悬浮框的提示文字
        trigger: "axis",
      },
      legend: {
        data: ["最高气温", "最低气温"],
      },
      xAxis: [
        {
          //x轴坐标数据
          type: "category",
          boundaryGap: false,
          data: ["周一", "周二", "周三", "周四", "周五", "周六", "周日"],
        },
      ],
      yAxis: [
        {
          //y轴坐标数据
          type: "value",
          axisLabel: {
            formatter: "{value} °C",
          },
        },
      ],
      series: [
        //驱动图表生成的数据内容数组,几条折现,数组中就会有几个对应对象,来表示对应的折线
        {
          name: "最高气温",
          type: "line", //pie->饼状图  line->折线图  bar->柱状图
          data: [11, 11, 15, 13, 12, 13, 10],
        },
        {
          name: "最低气温",
          type: "line", //pie->饼状图  line->折线图  bar->柱状图
          data: [1, -2, 2, 5, 3, 2, 0],
        },
      ],
    };
    myCharts.setOption(options);
  }
  mounted() {
    this.$nextTick(() => {
      this.init();
    });

  }

4,以上方法都不行的话检查一下 eCharts 的版本

"echarts": "^5.0", // 替换为4.8.0 || 查看官方推荐的引入方式,版本迭代引入方式有时间会有所不同
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值