From be6080ba0ffac71e430ae4d580c935f6479f17c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9D=92=E8=8F=9C=E7=99=BD=E7=8E=89=E6=B1=A4?= <79054161+Azir-11@users.noreply.github.com> Date: Mon, 10 Mar 2025 18:25:36 +0800 Subject: [PATCH] feat(utils): support replaceTab. (#713) --- src/hooks/common/router.ts | 9 ++------- src/store/modules/tab/index.ts | 20 ++++++++++++++++++++ src/typings/app.d.ts | 6 ++++++ 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/hooks/common/router.ts b/src/hooks/common/router.ts index 4ab21ea0..ac51dfc4 100644 --- a/src/hooks/common/router.ts +++ b/src/hooks/common/router.ts @@ -18,12 +18,7 @@ export function useRouterPush(inSetup = true) { const routerBack = router.back; - interface RouterPushOptions { - query?: Record; - params?: Record; - } - - async function routerPushByKey(key: RouteKey, options?: RouterPushOptions) { + async function routerPushByKey(key: RouteKey, options?: App.Global.RouterPushOptions) { const { query, params } = options || {}; const routeLocation: RouteLocationRaw = { @@ -67,7 +62,7 @@ export function useRouterPush(inSetup = true) { async function toLogin(loginModule?: UnionKey.LoginModule, redirectUrl?: string) { const module = loginModule || 'pwd-login'; - const options: RouterPushOptions = { + const options: App.Global.RouterPushOptions = { params: { module } diff --git a/src/store/modules/tab/index.ts b/src/store/modules/tab/index.ts index 85add103..0af5dd56 100644 --- a/src/store/modules/tab/index.ts +++ b/src/store/modules/tab/index.ts @@ -160,6 +160,25 @@ export const useTabStore = defineStore(SetupStoreId.Tab, () => { update(); } + const { routerPushByKey } = useRouterPush(); + /** + * Replace tab + * + * @param key Route key + * @param options Router push options + */ + async function replaceTab(key: RouteKey, options?: App.Global.RouterPushOptions) { + const oldTabId = activeTabId.value; + + // push new route + await routerPushByKey(key, options); + + // remove old tab (exclude fixed tab) + if (!isTabRetain(oldTabId)) { + await removeTab(oldTabId); + } + } + /** * Switch route by tab * @@ -282,6 +301,7 @@ export const useTabStore = defineStore(SetupStoreId.Tab, () => { removeTab, removeActiveTab, removeTabByRouteName, + replaceTab, clearTabs, clearLeftTabs, clearRightTabs, diff --git a/src/typings/app.d.ts b/src/typings/app.d.ts index cdfde34c..73cd76f1 100644 --- a/src/typings/app.d.ts +++ b/src/typings/app.d.ts @@ -175,6 +175,12 @@ declare namespace App { type RoutePath = import('@elegant-router/types').RoutePath; type LastLevelRouteKey = import('@elegant-router/types').LastLevelRouteKey; + /** The router push options */ + type RouterPushOptions = { + query?: Record; + params?: Record; + }; + /** The global header props */ interface HeaderProps { /** Whether to show the logo */