vue项目中使用echarts map报错Cannot read property 'push' of undefined nanhai.js

时间:2023-03-08 16:40:58
vue项目中使用echarts map报错Cannot read property 'push' of undefined  nanhai.js

在vue中绘制地图需要加载一个本地china.json文件,我用的是get请求的方法加载的,而不是直接import,因为我怕import请求到的部署到线上的时候会有问题。如下是get请求方法:

this.$http.get(zhongguo).then(res => {
console.log(res);
this.$echarts.registerMap("china", res); //注册地图
console.log("223442");
var myChart = this.$echarts.extendsMap("mapChart", { //绘制地图
bgColor: "", // 画布背景色
mapName: "china", // 地图名
text: "by:wxw",
goDown: true, // 是否下钻
// 下钻回调
callback: function(name, option, instance) {
console.log(name, option, instance);
},
// 数据展示
data: areaProjects
});
});

在请求到数据后我直接用res来绘制地图,结果报错

vue项目中使用echarts map报错Cannot read property 'push' of undefined  nanhai.js

我就纳闷,根本没有这个文件啊,但其实这个js文件就在我们安装的echarts包里面

报错行打断点:regions: undefined

vue项目中使用echarts map报错Cannot read property 'push' of undefined  nanhai.js

我看到有人说别用get请求方法,直接import就不会报错,嗯?为什么会这样?我打印了一下res

vue项目中使用echarts map报错Cannot read property 'push' of undefined  nanhai.js

这么多没用的数据,我们绘制地图只需要res.data,而不能是整个res,修改后就可以了展示地图了

this.$http.get(zhongguo).then(res => {
console.log(res);
this.$echarts.registerMap("china", res.data); //注册地图
console.log("223442");
var myChart = this.$echarts.extendsMap("mapChart", { //绘制地图
bgColor: "", // 画布背景色
mapName: "china", // 地图名
text: "by:wxw",
goDown: true, // 是否下钻
// 下钻回调
callback: function(name, option, instance) {
console.log(name, option, instance);
},
// 数据展示
data: areaProjects
});
});

就这个问题折腾了我两天时间,今天早上忽然开窍,真想给自己一个大嘴巴子