From b785bc5704e6299ec2e34b5839b40a2ea4d05401 Mon Sep 17 00:00:00 2001 From: Netfan Date: Sun, 12 Jan 2025 09:54:37 +0800 Subject: [PATCH] fix: useEcharts return invalid instance (#5360) --- .../effects/plugins/src/echarts/use-echarts.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/effects/plugins/src/echarts/use-echarts.ts b/packages/effects/plugins/src/echarts/use-echarts.ts index ec5786e8b..d2e96ebb6 100644 --- a/packages/effects/plugins/src/echarts/use-echarts.ts +++ b/packages/effects/plugins/src/echarts/use-echarts.ts @@ -2,6 +2,8 @@ import type { EChartsOption } from 'echarts'; import type { Ref } from 'vue'; +import type { Nullable } from '@vben/types'; + import type EchartsUI from './echarts-ui.vue'; import { computed, nextTick, watch } from 'vue'; @@ -50,7 +52,10 @@ function useEcharts(chartRef: Ref) { return chartInstance; }; - const renderEcharts = (options: EChartsOption, clear = true) => { + const renderEcharts = ( + options: EChartsOption, + clear = true, + ): Promise> => { cacheOptions = options; const currentOptions = { ...options, @@ -58,9 +63,8 @@ function useEcharts(chartRef: Ref) { }; return new Promise((resolve) => { if (chartRef.value?.offsetHeight === 0) { - useTimeoutFn(() => { - renderEcharts(currentOptions); - resolve(null); + useTimeoutFn(async () => { + resolve(await renderEcharts(currentOptions)); }, 30); return; } @@ -72,7 +76,7 @@ function useEcharts(chartRef: Ref) { } clear && chartInstance?.clear(); chartInstance?.setOption(currentOptions); - resolve(null); + resolve(chartInstance); }, 30); }); }); @@ -109,7 +113,7 @@ function useEcharts(chartRef: Ref) { return { renderEcharts, resize, - chartInstance, + getChartInstance: () => chartInstance, }; }