Vue3+echarts5踩坑以及resize方法報錯的解決
Vue3+echarts5踩坑resize方法報錯
項目要做一個數(shù)據(jù)統(tǒng)計可視化的模塊,按照官網(wǎng)做法調(diào)用resize方法時報錯:
卡了一整天,最后在官方issues上找到了原因,記錄一下。
原因
Vue3使用了proxy
,Echarts無法從中獲取內(nèi)部變量
解決方法
不要在data
中定義chart,或者使用shallowRef
mounted() { // 注:圖表不能放進data,vue3使用proxy,echarts無法從中獲取變量 let charts = [ { id: 'yearChart', chart: null, options: yearOption }, { id: 'monthChart', chart: null, options: monthOption } ] // 使用剛指定的配置項和數(shù)據(jù)顯示圖表。 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ù)據(jù),在全局監(jiān)聽resize事件時報錯
查看原因
發(fā)現(xiàn)是在vue3使用proxy監(jiān)聽的,Echarts無法從中獲取內(nèi)部變量
解決方法
不要在data中定義chart,或者使用shallowRef
<template> <button @click="handleClick">更換數(shù)據(jù)</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>
結(jié)果:
官方的issues:https://github.com/apache/echarts/issues/14781
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 在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ù)據(jù)的方式
- vue踩坑記錄之echarts動態(tài)數(shù)據(jù)刷新問題
- Vue中引入echarts的步驟及折線圖、柱狀圖常見配置項
相關(guān)文章
Vue中關(guān)于異步請求數(shù)據(jù)未更新的解決
本文將探討Vue中異步請求數(shù)據(jù)未更新的常見原因,并提供解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03vue單文件組件lint error自動fix與styleLint報錯自動fix詳解
這篇文章主要給大家介紹了關(guān)于vue單文件組件lint error自動fix與styleLint報錯自動fix的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-01-01Element-UI日期選擇器(選擇日期范圍)禁用未來日期實現(xiàn)代碼
我們在網(wǎng)頁開發(fā)時通常需要用到一些日期組件來方便用戶選擇時間,其中element日期組件是一個非常好用的工具,這篇文章主要給大家介紹了關(guān)于Element-UI日期選擇器(選擇日期范圍)禁用未來日期的相關(guān)資料,需要的朋友可以參考下2024-02-02vue跨域問題:Access?to?XMLHttpRequest?at‘httplocalhost解決
在前端發(fā)出Ajax請求的時候,有時候會產(chǎn)生跨域問題,下面這篇文章主要給大家介紹了關(guān)于vue跨域問題:Access?to?XMLHttpRequest?at‘httplocalhost的解決辦法,需要的朋友可以參考下2023-01-01vue使用Luckysheet插件實現(xiàn)excel導入導出
本文主要介紹了vue使用Luckysheet插件實現(xiàn)excel導入導出,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-03-03