地图 下钻时 ,同一个区域点击多次会出现 如下图 报错
原因是 每次引入的JSON文件都是新的,应该对绘图所需的JSON文件进行缓存
因为 echarts 会绘制解析 json 之后 执行 decode 方法 后 会将其 UTF8Encoding 的值 从 true 改为false,第二次绘制 时如果 为 false 则 不需要走 decode 方法,如果每次都是新引入的 json,那每次都走 decode 就会报错
// 问题代码
// chinaJson 中国地图json
// names => ['黑龙江', '辽宁', '吉林']
const newJson = Object.assign({}, chinaJson)
newJson.features = chinaJson.features.reduce((pre, nex) => {
if (names.includes(nex.properties.name)) {
pre.push(nex)
}
return pre
}, [])
echarts.registerMap(name, newJson)
// 解决代码
// 缓存计算后的json数据,如果计算过就不重新计算了
let regionsInfo = {}
if (!regionsInfo[name]) {
const newJson = Object.assign({}, chinaJson)
newJson.features = chinaJson.features.reduce((pre, nex) => {
if (names.includes(nex.properties.name)) {
pre.push(nex)
}
return pre
}, [])
regionsInfo[name] = newJson
}
echarts.registerMap(name, regionsInfo[name])
注意:
vue 获取本地json文件时,用 import 方式 获取
await import(’…/…/utils/city/’ + provincesMap[this.name] + ‘.json’)
此时需要 Object.assign({}, json) 拷贝一次
从文件中获取的是只读数据,不可更改,echarts 自然也没办法 decode之后更改值,也就会报错