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 { diff --git a/src/views/tab_example/index.vue b/src/views/tab_example/index.vue index a755ff2a..1a497cae 100755 --- a/src/views/tab_example/index.vue +++ b/src/views/tab_example/index.vue @@ -12,7 +12,7 @@ function open() { router.push(settingsStore.settings.home.fullPath) } function close() { - tabbar.close(settingsStore.settings.home.fullPath) + router.close(settingsStore.settings.home.fullPath) } function closeById() { tabbar.closeById()