diff --git a/src/router/extensions.ts b/src/router/extensions.ts new file mode 100644 index 00000000..941d92cf --- /dev/null +++ b/src/router/extensions.ts @@ -0,0 +1,64 @@ +import type { RouteLocationRaw, Router } from 'vue-router' +import pinia from '@/store' +import useSettingsStore from '@/store/modules/settings' +import useTabbarStore from '@/store/modules/tabbar' + +function getId(router: Router) { + return router.currentRoute.value.fullPath +} + +function extendPush(router: Router) { + const originalPush = router.push + router.push = function (to: RouteLocationRaw) { + const settingsStore = useSettingsStore(pinia) + if (settingsStore.settings.tabbar.enable) { + const tabbarStore = useTabbarStore(pinia) + const index = tabbarStore.list.findIndex(item => item.tabId === getId(router)) + tabbarStore.$patch({ + leaveIndex: index, + }) + } + return originalPush(to) + } +} + +function extendGo(router: Router) { + const originalGo = router.go + router.go = function (delta: number) { + const settingsStore = useSettingsStore(pinia) + if (settingsStore.settings.tabbar.enable) { + const tabId = getId(router) + const tabbarStore = useTabbarStore(pinia) + originalGo(delta) + if (delta < 0) { + tabbarStore.remove(tabId) + } + } + else { + originalGo(delta) + } + } +} + +function extendReplace(router: Router) { + const originalReplace = router.replace + router.replace = function (to: RouteLocationRaw) { + const settingsStore = useSettingsStore(pinia) + if (settingsStore.settings.tabbar.enable) { + const tabId = getId(router) + const tabbarStore = useTabbarStore(pinia) + return originalReplace(to).then(() => { + tabbarStore.remove(tabId) + }) + } + else { + return originalReplace(to) + } + } +} + +export default function setupExtensions(router: Router) { + extendPush(router) + extendGo(router) + extendReplace(router) +} diff --git a/src/router/index.ts b/src/router/index.ts index 81340eb9..5ef4e8b9 100755 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,8 +1,8 @@ import pinia from '@/store' import useSettingsStore from '@/store/modules/settings' import { loadingFadeOut } from 'virtual:app-loading' - import { createRouter, createWebHashHistory } from 'vue-router' +import setupExtensions from './extensions' import setupGuards from './guards' // 路由相关数据 import { constantRoutes, constantRoutesByFilesystem } from './routes' @@ -13,6 +13,7 @@ const router = createRouter({ }) setupGuards(router) +setupExtensions(router) router.isReady().then(() => { loadingFadeOut() diff --git a/src/utils/composables/useTabbar.ts b/src/utils/composables/useTabbar.ts index 3104c374..f21cc00d 100755 --- a/src/utils/composables/useTabbar.ts +++ b/src/utils/composables/useTabbar.ts @@ -12,27 +12,6 @@ export default function useTabbar() { return route.fullPath } - function open(to: RouteLocationRaw) { - const index = tabbarStore.list.findIndex(item => item.tabId === getId()) - tabbarStore.$patch({ - leaveIndex: index, - }) - router.push(to) - } - - function go(delta: number) { - const tabId = getId() - router.go(delta) - tabbarStore.remove(tabId) - } - - function replace(to: RouteLocationRaw) { - const tabId = getId() - router.replace(to).then(() => { - tabbarStore.remove(tabId) - }) - } - function close(to: RouteLocationRaw) { const tabId = getId() router.push(to).then(() => { @@ -170,9 +149,6 @@ export default function useTabbar() { return { getId, - open, - go, - replace, close, closeById, closeOtherSide, diff --git a/src/views/tab_example/index.vue b/src/views/tab_example/index.vue index 9225d6de..14d76803 100755 --- a/src/views/tab_example/index.vue +++ b/src/views/tab_example/index.vue @@ -7,11 +7,11 @@ meta: import useSettingsStore from '@/store/modules/settings' const settingsStore = useSettingsStore() - +const router = useRouter() const tabbar = useTabbar() function open() { - tabbar.open(settingsStore.settings.home.fullPath) + router.push(settingsStore.settings.home.fullPath) } function close() { tabbar.close(settingsStore.settings.home.fullPath)