From 448f3ba494a9ecc1a80d275a29b9a6dfbd3cb217 Mon Sep 17 00:00:00 2001 From: chansee97 Date: Tue, 4 Jun 2024 20:43:09 +0800 Subject: [PATCH] fix: perfect hooks --- service.config.ts | 6 +-- src/hooks/index.ts | 1 + src/hooks/refForm.ts | 15 ++++++ src/hooks/useEcharts.ts | 53 ++++++------------- .../dashboard/monitor/components/chart.vue | 4 +- .../dashboard/monitor/components/chart2.vue | 4 +- .../dashboard/monitor/components/chart3.vue | 3 +- .../dashboard/workbench/components/chart.vue | 3 +- src/views/demo/echarts/index.vue | 13 +++-- .../setting/account/components/TableModal.vue | 19 +++---- src/views/setting/account/index.vue | 2 +- .../setting/menu/components/TableModal.vue | 19 +++---- 12 files changed, 76 insertions(+), 66 deletions(-) create mode 100644 src/hooks/refForm.ts diff --git a/service.config.ts b/service.config.ts index 86b60a6..037d48e 100644 --- a/service.config.ts +++ b/service.config.ts @@ -1,12 +1,12 @@ /** 不同请求服务的环境配置 */ export const serviceConfig: Record> = { dev: { - url: 'https://mock.apifox.com/m1/4071143-0-default', + url: 'https://mock.apifox.cn/m1/4071143-0-default', }, test: { - url: 'https://mock.apifox.com/m1/4071143-0-default', + url: 'https://mock.apifox.cn/m1/4071143-0-default', }, prod: { - url: 'https://mock.apifox.com/m1/4071143-0-default', + url: 'https://mock.apifox.cn/m1/4071143-0-default', }, } diff --git a/src/hooks/index.ts b/src/hooks/index.ts index c42edf9..e601b77 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -1,3 +1,4 @@ export * from './useBoolean' export * from './useEcharts' export * from './usePermission' +export * from './refForm' diff --git a/src/hooks/refForm.ts b/src/hooks/refForm.ts new file mode 100644 index 0000000..a7f925c --- /dev/null +++ b/src/hooks/refForm.ts @@ -0,0 +1,15 @@ +export function refForm(initValue: T) { + function jsonClone(value: T) { + return JSON.parse(JSON.stringify(value)) + } + const target = ref(initValue) + + function setDefault() { + target.value = jsonClone(initValue) + } + + return { + target, + setDefault, + } +} diff --git a/src/hooks/useEcharts.ts b/src/hooks/useEcharts.ts index 715fb24..04de3ef 100644 --- a/src/hooks/useEcharts.ts +++ b/src/hooks/useEcharts.ts @@ -66,73 +66,54 @@ echarts.use([ /** * Echarts hooks函数 - * @param options - 图表配置 * @description 按需引入图表组件,没注册的组件需要先引入 */ -export function useEcharts(options: Ref) { +export function useEcharts(el: Ref, chartOptions: Ref) { const appStore = useAppStore() - const domRef = ref() - let chart: echarts.ECharts | null = null - const initialSize = { width: 0, height: 0 } - const { width, height } = useElementSize(domRef, initialSize) + const { width, height } = useElementSize(el) - function canRender() { - return initialSize.width > 0 && initialSize.height > 0 - } + const isRendered = computed(() => Boolean(el && chart)) - function isRendered() { - return Boolean(domRef.value && chart) - } async function render() { const chartTheme = appStore.colorMode ? 'dark' : 'light' await nextTick() - if (domRef.value) { - chart = echarts.init(domRef.value, chartTheme) - update(options.value) + if (el) { + chart = echarts.init(el.value, chartTheme) + update(chartOptions.value) } } function update(updateOptions: ECOption) { - if (isRendered()) + if (isRendered.value) chart!.setOption({ ...updateOptions, backgroundColor: 'transparent' }) } - function resize() { - chart?.resize() - } - function destroy() { chart?.dispose() chart = null } - const sizeWatch = watch([width, height], async ([newWidth, newHeight]) => { - initialSize.width = newWidth - initialSize.height = newHeight - if (newWidth === 0 && newHeight === 0) { - // 节点被删除 将chart置为空 - chart = null - } - if (!canRender()) - return - if (isRendered()) - resize() - else await render() + + watch([width, height], async ([newWidth, newHeight]) => { + if (isRendered.value && newWidth && newHeight) + chart?.resize() }) - const OptionWatch = watch(options, (newValue) => { + watch(chartOptions, (newValue) => { update(newValue) }) + onMounted(() => { + render() + }) onUnmounted(() => { - sizeWatch() - OptionWatch() destroy() }) return { - domRef, + destroy, + update, } } diff --git a/src/views/dashboard/monitor/components/chart.vue b/src/views/dashboard/monitor/components/chart.vue index 72911a7..7938410 100644 --- a/src/views/dashboard/monitor/components/chart.vue +++ b/src/views/dashboard/monitor/components/chart.vue @@ -113,7 +113,9 @@ const lineOptions = ref({ data: [20, 71, 8, 50, 57, 32], }], }) as Ref -const { domRef: lineRef } = useEcharts(lineOptions) + +const lineRef = ref(null) +useEcharts(lineRef, lineOptions)