首页 > 网络编程 > JavaScript > javascript类库 > vue.js > Echart报错Uncaught TypeError:Cannot read properties of undefined(reading ‘type‘)
解决Echart报错Uncaught TypeError:Cannot read properties of undefined(reading ‘type‘)问题
作者:毛三仙
使用ECharts时,通过ResizeObserver监听父元素宽高变化可能会导致报错,这是因为ECharts在Vue内部被转换为响应式对象,导致在resize时无法正确获取响应式API,解决方法是使用`markRaw`标记对象为不可响应,或者升级ECharts版本至5.3.2及以上,以修复相关bug
Echart报错Uncaught TypeError:Cannot read properties of undefined(reading ‘type‘)
报错情况,使用了ResizeObserver监听了echart父元素宽高变化,然后就出现了以下两种报错。
使用echart 报错 Uncaught TypeError: Cannot read properties of undefined (reading ‘type’)
报错截图

echart会被在vue内部转换成响应式对象,从而在resize 的时候获取不到
使用 vue3的API , markRaw,标记一个对象,使其不能成为一个响应式对象
import { markRaw } from "vue";
let myChart = ref(null)
let chartElement = document.getElementById(“chart”)
myChart.value = markRaw(echarts.init(chartElement))
鼠标放在图表上或者拖拽底部minMax栏也会报错;“echarts”: “^5.3.2”

示例代码
<!--
* @Description: 折线图组件 页面
* @Author: mhf
* @Date: 2024/3/12 17:46
-->
<template>
<div class="charts" :id="myChartId"></div>
</template>
<script>
import * as echarts from "echarts";
import { markRaw } from "vue";
export default {
name: "lineComp",
data() {
return {
myChart: null,
myChartId: "lineComp" + new Date().getTime(),
};
},
methods: {
initLine() {
var xData = (function () {
var data = [];
for (var i = 1; i < 13; i++) {
data.push(i + "月份");
}
return data;
})();
let option = {
backgroundColor: "#344b58",
title: {
text: "本年商场顾客男女人数统计",
subtext: "BY Wang Dingding",
x: "4%",
textStyle: {
color: "#fff",
fontSize: "22"
},
subtextStyle: {
color: "#90979c",
fontSize: "16"
}
},
tooltip: {
trigger: "axis",
axisPointer: {
type: "shadow",
textStyle: {
color: "#fff"
}
}
},
grid: {
borderWidth: 0,
top: 110,
bottom: 95,
textStyle: {
color: "#fff"
}
},
legend: {
x: "4%",
top: "8%",
textStyle: {
color: "#90979c"
},
data: ["女", "男", "平均"]
},
calculable: true,
xAxis: [
{
type: "category",
axisLine: {
lineStyle: {
color: "#90979c"
}
},
splitLine: {
show: false
},
axisTick: {
show: false
},
splitArea: {
show: false
},
axisLabel: {
interval: 0
},
data: xData
}
],
yAxis: [
{
type: "value",
splitLine: {
show: false
},
axisLine: {
lineStyle: {
color: "#90979c"
}
},
axisTick: {
show: false
},
axisLabel: {
interval: 0
},
splitArea: {
show: false
}
}
],
dataZoom: [
{
show: true,
height: 30,
xAxisIndex: [0],
bottom: 30,
start: 10,
end: 80,
handleIcon:
"path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z",
handleSize: "110%",
handleStyle: {
color: "#d3dee5"
},
textStyle: {
color: "#fff"
},
borderColor: "#90979c"
},
{
type: "inside",
show: true,
height: 15,
start: 1,
end: 35
}
],
series: [
{
name: "女",
type: "bar",
stack: "总量",
barMaxWidth: 35,
barGap: "10%",
itemStyle: {
normal: {
color: "rgba(255,144,128,1)",
label: {
show: true,
textStyle: {
color: "#fff"
},
position: "inside",
formatter: function (p) {
return p.value > 0 ? p.value : "";
}
}
}
},
data: [
709, 1917, 2455, 2610, 1719, 1433, 1544, 3285, 5208, 3372, 2484,
4078
]
},
{
name: "男",
type: "bar",
stack: "总量",
itemStyle: {
normal: {
color: "rgba(0,191,183,1)",
barBorderRadius: 0,
label: {
show: true,
position: "inside",
formatter: function (p) {
return p.value > 0 ? p.value : "";
}
}
}
},
data: [
327, 1776, 507, 1200, 800, 482, 204, 1390, 1001, 951, 381, 220
]
},
{
name: "总数",
type: "line",
symbolSize: 10,
symbol: "circle",
itemStyle: {
normal: {
color: "rgba(252,230,48,1)",
barBorderRadius: 0,
label: {
show: true,
position: "top",
formatter: function (p) {
return p.value > 0 ? p.value : "";
}
}
}
},
data: [
1036, 3693, 2962, 3810, 2519, 1915, 1748, 4675, 6209, 4323, 2865,
4298
]
}
]
};
this.myChart = markRaw(
echarts.init(document.getElementById(this.myChartId))
)
this.myChart.setOption(option);
window.addEventListener("resize", () => {
this.myChart.resize();
});
},
dispose() {
this.myChart.dispose();
}
},
created() {},
mounted() {
this.initLine();
},
beforeDestroy() {
if (this.myChart) {
this.myChart.dispose();
}
}
};
</script>
<style lang="scss" scoped>
$blue: #1492ff;
.charts {
width: 100%;
min-height: 500px !important;
}
</style>
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- vite+vue3项目报错:TypeError: Promise.allSettled is not a function
- Vue报错:TypeError:Cannot create property 'xxx' on string 'xxxx'问题
- React/vue开发报错TypeError:this.getOptions is not a function的解决
- vue watch报错:Error in callback for watcher "xxx":"TypeError的解决方法
- 完美解决vue中报错 “TypeError: Cannot read properties of null (reading'forEach')“
