From 59e683c2e334f1e30b98ce7dd343bf6f98ac9cda Mon Sep 17 00:00:00 2001 From: Hooray <304327508@qq.com> Date: Fri, 20 Jun 2025 18:42:01 +0800 Subject: [PATCH] =?UTF-8?q?refactor!:=20=E6=89=A9=E5=B1=95=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=20`close`=20=E5=87=BD=E6=95=B0=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=20useTabbar=20=E4=B8=AD=20`close`=20?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/extensions.ts | 22 ++++++++++++++++++++++ src/utils/composables/useTabbar.ts | 12 ++---------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/router/extensions.ts b/src/router/extensions.ts index f024db2c..8f0c5d93 100644 --- a/src/router/extensions.ts +++ b/src/router/extensions.ts @@ -55,8 +55,30 @@ function extendReplace(router: Router) { } } +declare module 'vue-router' { + interface Router { + /** + * 本方法为框架扩展语法,等同于 `push` 方法,并且同时会关闭当前标签页 + */ + close: (to: RouteLocationRaw) => Promise + } +} + +function extendClose(router: Router) { + router.close = function (to: RouteLocationRaw) { + const settingsStore = useSettingsStore(pinia) + if (settingsStore.settings.tabbar.enable) { + const tabId = getId(router) + const tabbarStore = useTabbarStore(pinia) + tabbarStore.remove(tabId) + } + return router.push(to) + } +} + export default function setupExtensions(router: Router) { extendPush(router) extendGo(router) extendReplace(router) + extendClose(router) } diff --git a/src/utils/composables/useTabbar.ts b/src/utils/composables/useTabbar.ts index 5dac3215..503e5332 100755 --- a/src/utils/composables/useTabbar.ts +++ b/src/utils/composables/useTabbar.ts @@ -1,4 +1,3 @@ -import type { RouteLocationRaw } from 'vue-router' import { toast } from 'vue-sonner' export default function useTabbar() { @@ -11,13 +10,6 @@ export default function useTabbar() { return route.fullPath } - function close(to: RouteLocationRaw) { - const tabId = getId() - router.push(to).then(() => { - tabbarStore.remove(tabId) - }) - } - function closeById(tabId = getId()) { if (checkClose(tabId, false)) { const activedTabId = getId() @@ -25,10 +17,10 @@ export default function useTabbar() { if (tabId === activedTabId) { const index = tabbarStore.list.findIndex(item => item.tabId === tabId) if (index > 0) { - close(tabbarStore.list[index - 1].fullPath) + router.close(tabbarStore.list[index - 1].fullPath) } else { - close(tabbarStore.list[index + 1].fullPath) + router.close(tabbarStore.list[index + 1].fullPath) } } else {