Vue引入echarts.js,初始化init()遇到的坑。
报错一: Cannot read property ‘init’ of undefined
报未定义错误,说明echarts未定义,echarts未引入成功。引入低版本未解决,只需要将引入方式修改:
import echarts from 'echarts'; //修改为 import * as echarts from 'echarts';
复制
报错二: echarts.init is not a function
报 init 不是方法,说明 echarts 已经定义,可以用测试变量z接受echarts,查看一下 echarts 的值:
this.z = this.$echarts; //本例使用的是全局定义的echarts
复制
发现 echarts 外面多包了一层:
所以,只需要多索引一层即可:
var myChart = this.$echarts.init(document.getElementById('dom_Id')); //修改为 var myChart = this.$echarts.echarts.init(document.getElementById('dom_Id'));
复制