Skip to content

Commit 924b968

Browse files
committed
feat(projects): support replaceTab.
1 parent 3e0076d commit 924b968

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

src/hooks/common/router.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@ export function useRouterPush(inSetup = true) {
1818

1919
const routerBack = router.back;
2020

21-
interface RouterPushOptions {
22-
query?: Record<string, string>;
23-
params?: Record<string, string>;
24-
}
25-
26-
async function routerPushByKey(key: RouteKey, options?: RouterPushOptions) {
21+
async function routerPushByKey(key: RouteKey, options?: App.Global.RouterPushOptions) {
2722
const { query, params } = options || {};
2823

2924
const routeLocation: RouteLocationRaw = {
@@ -67,7 +62,7 @@ export function useRouterPush(inSetup = true) {
6762
async function toLogin(loginModule?: UnionKey.LoginModule, redirectUrl?: string) {
6863
const module = loginModule || 'pwd-login';
6964

70-
const options: RouterPushOptions = {
65+
const options: App.Global.RouterPushOptions = {
7166
params: {
7267
module
7368
}

src/store/modules/tab/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,24 @@ export const useTabStore = defineStore(SetupStoreId.Tab, () => {
160160
update();
161161
}
162162

163+
const { routerPushByKey } = useRouterPush();
164+
/**
165+
* Replace tab
166+
*
167+
* @param route Tab route
168+
*/
169+
async function replaceTab(key: RouteKey, options?: App.Global.RouterPushOptions) {
170+
const oldTabId = activeTabId.value;
171+
172+
// push new route
173+
await routerPushByKey(key, options);
174+
175+
// remove old tab (exclude fixed tab)
176+
if (!isTabRetain(oldTabId)) {
177+
await removeTab(oldTabId);
178+
}
179+
}
180+
163181
/**
164182
* Switch route by tab
165183
*
@@ -282,6 +300,7 @@ export const useTabStore = defineStore(SetupStoreId.Tab, () => {
282300
removeTab,
283301
removeActiveTab,
284302
removeTabByRouteName,
303+
replaceTab,
285304
clearTabs,
286305
clearLeftTabs,
287306
clearRightTabs,

src/typings/app.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ declare namespace App {
175175
type RoutePath = import('@elegant-router/types').RoutePath;
176176
type LastLevelRouteKey = import('@elegant-router/types').LastLevelRouteKey;
177177

178+
/** The router push options */
179+
type RouterPushOptions = {
180+
query?: Record<string, string>;
181+
params?: Record<string, string>;
182+
};
183+
178184
/** The global header props */
179185
interface HeaderProps {
180186
/** Whether to show the logo */

0 commit comments

Comments
 (0)