解決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在構(gòu)建時使用魔法糖語法時defineProps和defineEmits的注意事項小結(jié)
在 Vue 3.2+ 版本中,可以使用 <script setup> 替代傳統(tǒng)的 script標簽來編寫組件,它提供了更簡潔的語法來編寫 Composition API 代碼,這篇文章主要介紹了vue3在構(gòu)建時使用魔法糖語法時defineProps和defineEmits的注意事項小結(jié),需要的朋友可以參考下2024-04-04
淺析webpack-bundle-analyzer在vue-cli3中的使用
這篇文章主要介紹了webpack-bundle-analyzer在vue-cli3中的使用,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10
vue使用webPack打包發(fā)布后頁面顯示空白的解決
這篇文章主要介紹了vue使用webPack打包發(fā)布后頁面顯示空白的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Vue請求JSON Server服務器數(shù)據(jù)的實現(xiàn)方法
這篇文章主要介紹了Vue請求JSON Server服務器數(shù)據(jù)的實現(xiàn)方法,需要的朋友可以參考下2018-11-11
Vue自定義指令結(jié)合阿里云OSS優(yōu)化圖片的實現(xiàn)方法
這篇文章主要介紹了Vue自定義指令結(jié)合阿里云OSS優(yōu)化圖片的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
關(guān)于Element-ui中Table表格無法顯示的問題及解決
這篇文章主要介紹了關(guān)于Element-ui中Table表格無法顯示的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08

