最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決Vue中使用Echarts出現(xiàn)There?is?a?chart?instance?already?initialized?on?the?dom的警告問題

 更新時間:2023年06月19日 09:52:31   作者:唐志遠  
使用echarts的時候,多次加載會出現(xiàn)There?is?a?chart?instance?already?initialized?on?the?dom.這個黃色警告,此警告信息不影響echarts正常加載,但是有bug得解決,本文就帶大家解決這個問題,感興趣的同學可以參考閱讀

問題描述

使用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)文章

最新評論

牡丹江市| 弥渡县| 苏尼特右旗| 沙洋县| 板桥市| 海原县| 梁河县| 贞丰县| 石棉县| 永修县| 深水埗区| 广安市| 黄石市| 九台市| 都匀市| 咸宁市| 永仁县| 泊头市| 福州市| 县级市| 德化县| 上栗县| 沾化县| 株洲县| 富锦市| 方山县| 唐海县| 吉木萨尔县| 顺平县| 康定县| 平原县| 环江| 镇安县| 碌曲县| 万源市| 古丈县| 公主岭市| 阳山县| 定陶县| 焉耆| 镶黄旗|