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

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

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

問題描述

使用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
>;
// 注冊(cè)必須的組件
echarts.use([
    LegendComponent,
    TitleComponent,
    TooltipComponent,
    GridComponent,
    DatasetComponent,
    TransformComponent,
    BarChart,
    LineChart,
    PieChart,
    LabelLayout,
    UniversalTransition,
    CanvasRenderer
]);
export default echarts;

解決方法

在方法最外層定義echarts dom對(duì)象,然后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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

长寿区| 鄂温| 磐石市| 海盐县| 基隆市| 郑州市| 大石桥市| 体育| 延寿县| 霞浦县| 华亭县| 湘西| 穆棱市| 保康县| 普格县| 柯坪县| 呼图壁县| 闻喜县| 宣恩县| 广昌县| 乐都县| 曲松县| 桑植县| 牟定县| 大连市| 佳木斯市| 利辛县| 当涂县| 达尔| 界首市| 东辽县| 新乐市| 阿克| 启东市| 土默特左旗| 安阳市| 额敏县| 威信县| 二连浩特市| 水富县| 玉溪市|