Vue3+echarts5踩坑以及resize方法報錯的解決
Vue3+echarts5踩坑resize方法報錯
項目要做一個數(shù)據統(tǒng)計可視化的模塊,按照官網做法調用resize方法時報錯:

卡了一整天,最后在官方issues上找到了原因,記錄一下。
原因
Vue3使用了proxy,Echarts無法從中獲取內部變量
解決方法
不要在data中定義chart,或者使用shallowRef
mounted() {
// 注:圖表不能放進data,vue3使用proxy,echarts無法從中獲取變量
let charts = [
{ id: 'yearChart', chart: null, options: yearOption },
{ id: 'monthChart', chart: null, options: monthOption }
]
// 使用剛指定的配置項和數(shù)據顯示圖表。
this.$nextTick(() => {
this.charts.forEach(p => {
p.chart = echarts.init(document.getElementById(p.id));
p.chart.setOption(p.options);
})
})
// 監(jiān)聽窗口變化,重繪圖表
window.addEventListener("resize", (() => {
this.charts.forEach(p => {
setTimeout(() => { // 避免多圖同時加載卡頓
p.chart.resize();
}, 200)
})
}));
},參考:官方issues
vue3+echarts踩坑小計
想寫一下vue3和echarts5寫個小demo,然后實現(xiàn)一個小功能,點擊按鈕進行切換圖標數(shù)據,在全局監(jiān)聽resize事件時報錯

查看原因
發(fā)現(xiàn)是在vue3使用proxy監(jiān)聽的,Echarts無法從中獲取內部變量
解決方法
不要在data中定義chart,或者使用shallowRef
<template>
<button @click="handleClick">更換數(shù)據</button>
<div id="home" ref="home">
Home
</div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref, shallowRef } from 'vue';
import {ECharts, EChartsOption,init} from 'echarts';
// import useCurrentInstance from "@/utils/useCurrentInstance";
// const { proxy } = useCurrentInstance()
const myChart = shallowRef<ECharts>()
const home = ref<HTMLElement>()
const myData = reactive([
{id:1,name:'襯衫',xl:22,kc:77},
{id:2,name:'雪紡衫',xl:99,kc:45},
{id:3,name:'羊毛衫',xl:45,kc:11},
{id:4,name:'高跟鞋',xl:15,kc:23},
{id:5,name:'襪子',xl:12,kc:85},
{id:6,name:'帽子',xl:12,kc:85},
{id:7,name:'',xl:12,kc:85},
])
onMounted(() =>{
const charEle = document.getElementById('home') as HTMLElement;
myChart.value = init(charEle)
initOptions()
window.addEventListener('resize',() =>{
if(myChart.value){
myChart.value.resize()
}
})
})
const initOptions = () =>{
const option: EChartsOption = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
data: [150, 230, 224, 218, 135, 147, 260],
type: 'line'
}
],
legend:{}
};
(myChart.value as ECharts ).setOption(option)
}
const handleClick = () =>{
(myChart.value as ECharts ).setOption({
xAxis:{
data:myData.map(item => item.name)
},
series:[
{
name:'銷量',
type:'bar',
data:myData.map(item => item.xl)
},
{
name:'庫存',
type:'bar',
data:myData.map(item => item.kc)
}
]
})
}
</script>
<style scoped>
#home{
width: 95%;
height: 500px;
border: red solid 1px;
}
</style>結果:


官方的issues:https://github.com/apache/echarts/issues/14781
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 在Vue中使用Echarts+封裝
- vue+elementUI中使用Echarts之餅圖問題
- 手把手教你Vue3?按需引入?Echarts的過程(收藏)
- vue使用echarts如何實現(xiàn)雙柱狀圖和雙y軸的柱狀圖
- Vue3中按需引入ECharts詳細步驟(一看就會)
- vue3.x對echarts的二次封裝之按需加載過程詳解
- vue之使用echarts圖表setOption多次很卡問題及解決
- vue中echarts視圖不更新問題及解決
- Vue動態(tài)加載ECharts圖表數(shù)據的方式
- vue踩坑記錄之echarts動態(tài)數(shù)據刷新問題
- Vue中引入echarts的步驟及折線圖、柱狀圖常見配置項
相關文章
詳解vue-cli+element-ui樹形表格(多級表格折騰小計)
這篇文章主要介紹了vue-cli + element-ui 樹形表格(多級表格折騰小計),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
Vue Form表單的使用方法(rules格式校驗網絡校驗鍵盤按鍵監(jiān)聽)
本文介紹了在Vue.js中使用表單校驗規(guī)則(rules)進行網絡請求校驗的方法,以及如何通過formRef引用表單對象并進行鍵盤按鍵監(jiān)聽,感興趣的朋友跟隨小編一起看看吧2024-11-11

