解決Echart報(bào)錯(cuò)Uncaught?TypeError:Cannot?read?properties?of?undefined(reading?‘type‘)問(wèn)題
Echart報(bào)錯(cuò)Uncaught TypeError:Cannot read properties of undefined(reading ‘type‘)
報(bào)錯(cuò)情況,使用了ResizeObserver監(jiān)聽(tīng)了echart父元素寬高變化,然后就出現(xiàn)了以下兩種報(bào)錯(cuò)。
使用echart 報(bào)錯(cuò) Uncaught TypeError: Cannot read properties of undefined (reading ‘type’)
報(bào)錯(cuò)截圖

echart會(huì)被在vue內(nèi)部轉(zhuǎn)換成響應(yīng)式對(duì)象,從而在resize 的時(shí)候獲取不到
使用 vue3的API , markRaw,標(biāo)記一個(gè)對(duì)象,使其不能成為一個(gè)響應(yīng)式對(duì)象
import { markRaw } from "vue";
let myChart = ref(null)
let chartElement = document.getElementById(“chart”)
myChart.value = markRaw(echarts.init(chartElement))
鼠標(biāo)放在圖表上或者拖拽底部minMax欄也會(huì)報(bào)錯(cuò);“echarts”: “^5.3.2”

示例代碼
<!--
* @Description: 折線圖組件 頁(yè)面
* @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: "本年商場(chǎng)顧客男女人數(shù)統(tǒng)計(jì)",
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: "總數(shù)",
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>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vite+vue3項(xiàng)目報(bào)錯(cuò):TypeError:?Promise.allSettled?is?not?a?function
- Vue報(bào)錯(cuò):TypeError:Cannot create property 'xxx' on string 'xxxx'問(wèn)題
- React/vue開發(fā)報(bào)錯(cuò)TypeError:this.getOptions?is?not?a?function的解決
- vue?watch報(bào)錯(cuò):Error?in?callback?for?watcher?"xxx":"TypeError的解決方法
- 完美解決vue中報(bào)錯(cuò)?“TypeError:?Cannot?read?properties?of?null?(reading'forEach')“
相關(guān)文章
Vue3優(yōu)雅的實(shí)現(xiàn)跨組件通信的常用方法總結(jié)
開發(fā)中經(jīng)常會(huì)遇到跨組件通信的場(chǎng)景,props?逐層傳遞的方法實(shí)在是太不優(yōu)雅了,所以今天總結(jié)下可以更加簡(jiǎn)單的跨組件通信的一些方法,文中通過(guò)代碼實(shí)例講解的非常詳細(xì),需要的朋友可以參考下2023-11-11
Vue關(guān)于Element UI中的文本域換行問(wèn)題
這篇文章主要介紹了Vue關(guān)于Element UI中的文本域換行問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue-cli 3.0 引入mint-ui報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了vue-cli 3.0 引入mint-ui報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
Vue使用VueUse構(gòu)建一個(gè)支持暫停/重置的CountUp組件
本文介紹了如何利用 VueUse 的 useRafFn 從零構(gòu)建一個(gè)功能更強(qiáng)大的 CountUp 組件,它不僅支持 vue-countup-v3 的全部特性,還額外提供了 pause、resume、reset 等命令式控制方法,感興趣的小伙伴可以了解下2026-04-04
教你如何開發(fā)Vite3插件構(gòu)建Electron開發(fā)環(huán)境
這篇文章主要介紹了如何開發(fā)Vite3插件構(gòu)建Electron開發(fā)環(huán)境,文中給大家提到了如何讓 Vite 加載 Electron 的內(nèi)置模塊和 Node.js 的內(nèi)置模塊,需要的朋友可以參考下2022-11-11
vue2 element 彈出框拖拽會(huì)出現(xiàn)一層陰影問(wèn)題解決方法
這篇文章主要介紹了vue2 element 彈出框拖拽會(huì)出現(xiàn)一層陰影問(wèn)題解決方法,因增加 draggable 屬性導(dǎo)致我彈窗表單清空文本框時(shí),從右向左選中字體會(huì)出現(xiàn)拖拽陰影效果,本文給大家介紹vue2 element 彈出框拖拽會(huì)出現(xiàn)一層陰影問(wèn)題解決方法,感興趣的朋友一起看看吧2024-01-01
vue3的setup語(yǔ)法如何自定義v-model為公用hooks
這篇文章主要介紹了vue3的setup語(yǔ)法如何自定義v-model為公用hooks,文章分為兩個(gè)部分介紹,簡(jiǎn)單介紹vue3的setup語(yǔ)法如何自定義v-model和如何提取v-model語(yǔ)法作為一個(gè)公用hooks2022-07-07

