解決Vue中使用Echarts出現(xiàn)There?is?a?chart?instance?already?initialized?on?the?dom的警告問題
問題描述
使用echarts的時候,多次加載會出現(xiàn)There is a chart instance already initialized on the dom.這個黃色警告,大概意思就是dom上已經(jīng)初始化了一個圖表實例。此警告信息不影響echarts正常加載,但是有bug不解決的話,心里癢的慌!
先說明一下,echarts是用在了子組件的彈窗里,然后在父組件打開彈窗時調(diào)用echarts.init()的初始化方法。第一次渲染正常,之后再打開彈窗控制臺就會報There is a chart instance already initialized on the dom.


父組件中的代碼:
const taskDetailDom = ref()
const open = ()=> {
if (taskDetailDom.value) {
taskDetailDom.value.initEchart()
}
}如何造成的? 這里只區(qū)別了子組件的寫法。
錯誤寫法:
<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 來組合出一個只有必須組件和圖表的 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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解vue3.2新增的defineCustomElement底層原理
本文主要介紹了vue3.2新增的defineCustomElement底層原理,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
解讀element?el-upload上傳的附件名稱不顯示?file-list賦值
這篇文章主要介紹了解讀element?el-upload上傳的附件名稱不顯示?file-list賦值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
crypto-js對稱加密解密的使用方式詳解(vue與java端)
這篇文章主要介紹了如何在Vue前端和Java后端使用crypto-js庫進行AES加密和解密,前端通過創(chuàng)建AES.js文件來實現(xiàn)加密解密功能,并在Vue文件或JavaScript中使用,后端則可以直接使用Java代碼進行AES加密和解密操作,需要的朋友可以參考下2025-01-01
vue3.0搭配.net core實現(xiàn)文件上傳組件
這篇文章主要介紹了vue3.0搭配.net core實現(xiàn)文件上傳組件,幫助大家開發(fā)Web應(yīng)用程序,完成需求,感興趣的朋友可以了解下2020-10-10

