From 80351516ced0ec2d67c30405d4a644aca8ca4bc2 Mon Sep 17 00:00:00 2001 From: kailong321200875 <321200875@qq.com> Date: Wed, 20 Dec 2023 10:37:57 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E5=86=99useEventBus?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/event/{useEmitt.ts => useEventBus.ts} | 7 +++++-- src/views/Example/Page/ExampleAdd.vue | 8 ++++---- src/views/Example/Page/ExampleEdit.vue | 8 ++++---- src/views/Example/Page/ExamplePage.vue | 6 +++--- 4 files changed, 16 insertions(+), 13 deletions(-) rename src/hooks/event/{useEmitt.ts => useEventBus.ts} (69%) diff --git a/src/hooks/event/useEmitt.ts b/src/hooks/event/useEventBus.ts similarity index 69% rename from src/hooks/event/useEmitt.ts rename to src/hooks/event/useEventBus.ts index 26b5d62..d9ee832 100644 --- a/src/hooks/event/useEmitt.ts +++ b/src/hooks/event/useEventBus.ts @@ -8,7 +8,7 @@ interface Option { const emitter = mitt() -export const useEmitt = (option?: Option) => { +export const useEventBus = (option?: Option) => { if (option) { emitter.on(option.name, option.callback) @@ -18,6 +18,9 @@ export const useEmitt = (option?: Option) => { } return { - emitter + on: emitter.on, + off: emitter.off, + emit: emitter.emit, + all: emitter.all } } diff --git a/src/views/Example/Page/ExampleAdd.vue b/src/views/Example/Page/ExampleAdd.vue index 37480a8..3903e21 100644 --- a/src/views/Example/Page/ExampleAdd.vue +++ b/src/views/Example/Page/ExampleAdd.vue @@ -5,9 +5,9 @@ import { ref, unref } from 'vue' import { useI18n } from '@/hooks/web/useI18n' import { useRouter } from 'vue-router' import { saveTableApi } from '@/api/table' -import { useEmitt } from '@/hooks/event/useEmitt' +import { useEventBus } from '@/hooks/event/useEventBus' -const { emitter } = useEmitt() +const { emit } = useEventBus() const { push, go } = useRouter() @@ -28,7 +28,7 @@ const save = async () => { loading.value = false }) if (res) { - emitter.emit('getList', 'add') + emit('getList', 'add') push('/example/example-page') } } @@ -49,4 +49,4 @@ const save = async () => { -@/hooks/event/useEmitt +@/hooks/event/useEventBus diff --git a/src/views/Example/Page/ExampleEdit.vue b/src/views/Example/Page/ExampleEdit.vue index 8484995..eb65721 100644 --- a/src/views/Example/Page/ExampleEdit.vue +++ b/src/views/Example/Page/ExampleEdit.vue @@ -6,9 +6,9 @@ import { useI18n } from '@/hooks/web/useI18n' import { useRouter, useRoute } from 'vue-router' import { saveTableApi, getTableDetApi } from '@/api/table' import { TableData } from '@/api/table/types' -import { useEmitt } from '@/hooks/event/useEmitt' +import { useEventBus } from '@/hooks/event/useEventBus' -const { emitter } = useEmitt() +const { emit } = useEventBus() const { push, go } = useRouter() @@ -42,7 +42,7 @@ const save = async () => { loading.value = false }) if (res) { - emitter.emit('getList', 'editor') + emit('getList', 'editor') push('/example/example-page') } } @@ -63,4 +63,4 @@ const save = async () => { -@/hooks/event/useEmitt +@/hooks/event/useEventBus diff --git a/src/views/Example/Page/ExamplePage.vue b/src/views/Example/Page/ExamplePage.vue index 64b9804..c13fe3c 100644 --- a/src/views/Example/Page/ExamplePage.vue +++ b/src/views/Example/Page/ExamplePage.vue @@ -9,7 +9,7 @@ import { useTable } from '@/hooks/web/useTable' import { TableData } from '@/api/table/types' import { reactive, ref, unref } from 'vue' import { useRouter } from 'vue-router' -import { useEmitt } from '@/hooks/event/useEmitt' +import { useEventBus } from '@/hooks/event/useEventBus' import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas' import { BaseButton } from '@/components/Button' @@ -50,7 +50,7 @@ const { getList, getElTableExpose, delList } = tableMethods getList() -useEmitt({ +useEventBus({ name: 'getList', callback: (type: string) => { if (type === 'add') { @@ -294,4 +294,4 @@ const action = (row: TableData, type: string) => { /> -@/hooks/event/useEmitt +@/hooks/event/useEventBus