解決Vue中使用Echarts出現(xiàn)There?is?a?chart?instance?already?initialized?on?the?dom的警告問題
問題描述
使用echarts
的時(shí)候,多次加載會(huì)出現(xiàn)There is a chart instance already initialized on the dom.
這個(gè)黃色警告,大概意思就是dom
上已經(jīng)初始化了一個(gè)圖表實(shí)例。此警告信息不影響echarts
正常加載,但是有bug
不解決的話,心里癢的慌!
先說明一下,echarts
是用在了子組件的彈窗里,然后在父組件打開彈窗時(shí)調(diào)用echarts.init()
的初始化方法。第一次渲染正常,之后再打開彈窗控制臺(tái)就會(huì)報(bào)There is a chart instance already initialized on the dom.
父組件中的代碼:
const taskDetailDom = ref() const open = ()=> { if (taskDetailDom.value) { taskDetailDom.value.initEchart() } }
如何造成的? 這里只區(qū)別了子組件的寫法。
錯(cuò)誤寫法:
<script setup lang='ts'> import echarts from "@/utils/custom/echart" let tChart: Ref<HTMLDivElement | null> = ref(null) const initEchart = () => { const dom = tChart.value if (dom) { let myChart = echarts.init(dom) myChart.setOption(option) } } defineExpose({ initEchart })
關(guān)于import echarts from "@/utils/custom/echart"
此處中的代碼(可參考官方示例)如下:
import * as echarts from 'echarts/core'; import { BarChart, LineChart, PieChart } from 'echarts/charts'; import { LegendComponent, TitleComponent, TooltipComponent, GridComponent, // 數(shù)據(jù)集組件 DatasetComponent, // 內(nèi)置數(shù)據(jù)轉(zhuǎn)換器組件 (filter, sort) TransformComponent } from 'echarts/components'; import { LabelLayout, UniversalTransition } from 'echarts/features'; import { CanvasRenderer } from 'echarts/renderers'; import type { // 系列類型的定義后綴都為 SeriesOption BarSeriesOption, LineSeriesOption } from 'echarts/charts'; import type { // 組件類型的定義后綴都為 ComponentOption LegendComponentOption, TitleComponentOption, TooltipComponentOption, GridComponentOption, DatasetComponentOption } from 'echarts/components'; import type { ComposeOption, } from 'echarts/core'; // 通過 ComposeOption 來組合出一個(gè)只有必須組件和圖表的 Option 類型 type ECOption = ComposeOption< | BarSeriesOption | LegendComponentOption | LineSeriesOption | TitleComponentOption | TooltipComponentOption | GridComponentOption | DatasetComponentOption >; // 注冊必須的組件 echarts.use([ LegendComponent, TitleComponent, TooltipComponent, GridComponent, DatasetComponent, TransformComponent, BarChart, LineChart, PieChart, LabelLayout, UniversalTransition, CanvasRenderer ]); export default echarts;
解決方法
在方法最外層定義echarts dom
對象,然后echarts.init()
之前,判斷dom
是否為空或未定義,如果已存在則調(diào)用dispose()
方法銷毀,再初始化echarts.init()
。
let tChart: Ref<HTMLDivElement | null> = ref(null) let myChart: any // 1. 最外層定義 echarts dom const initEchart = () => { const dom = tChart.value if (dom) { // 2. 判斷 dom 是否為空或未定義 if (myChart != null && myChart != "" && myChart != undefined) { // 3. 已存在則調(diào)用 dispose() 方法銷毀 myChart.dispose(); } myChart = echarts.init(dom) myChart.setOption(option) } } defineExpose({ initEchart })
到此這篇關(guān)于解決Vue中使用Echarts出現(xiàn)There is a chart instance already initialized on the dom的警告問題的文章就介紹到這了,更多相關(guān)Vue中使用Echarts出現(xiàn)的問題內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue踩坑記錄之echarts動(dòng)態(tài)數(shù)據(jù)刷新問題
- Vue+ECharts+高德地圖API實(shí)現(xiàn)天氣預(yù)報(bào)數(shù)據(jù)可視化的教程
- Vue?Echarts報(bào)錯(cuò)Initialize?failed:?invalid?dom解決方法
- vue+echarts繪制省份地圖并添加自定義標(biāo)注方式
- vue3+ts使用Echarts的實(shí)例詳解
- vue中如何使用echarts和echarts-gl實(shí)現(xiàn)3D餅圖環(huán)形餅圖
- vue?echarts移動(dòng)端踩坑解決記錄
相關(guān)文章
詳解vue3.2新增的defineCustomElement底層原理
本文主要介紹了vue3.2新增的defineCustomElement底層原理,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08解讀element?el-upload上傳的附件名稱不顯示?file-list賦值
這篇文章主要介紹了解讀element?el-upload上傳的附件名稱不顯示?file-list賦值問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10vue點(diǎn)擊標(biāo)簽切換選中及互相排斥操作
這篇文章主要介紹了vue點(diǎn)擊標(biāo)簽切換選中及互相排斥操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07crypto-js對稱加密解密的使用方式詳解(vue與java端)
這篇文章主要介紹了如何在Vue前端和Java后端使用crypto-js庫進(jìn)行AES加密和解密,前端通過創(chuàng)建AES.js文件來實(shí)現(xiàn)加密解密功能,并在Vue文件或JavaScript中使用,后端則可以直接使用Java代碼進(jìn)行AES加密和解密操作,需要的朋友可以參考下2025-01-01vue3.0搭配.net core實(shí)現(xiàn)文件上傳組件
這篇文章主要介紹了vue3.0搭配.net core實(shí)現(xiàn)文件上傳組件,幫助大家開發(fā)Web應(yīng)用程序,完成需求,感興趣的朋友可以了解下2020-10-10