Skip to content

Commit be6080b

Browse files
authored
feat(utils): support replaceTab. (#713)
1 parent 3e0076d commit be6080b

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

Diff for: src/hooks/common/router.ts

+2-7
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
}

Diff for: src/store/modules/tab/index.ts

+20
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,25 @@ export const useTabStore = defineStore(SetupStoreId.Tab, () => {
160160
update();
161161
}
162162

163+
const { routerPushByKey } = useRouterPush();
164+
/**
165+
* Replace tab
166+
*
167+
* @param key Route key
168+
* @param options Router push options
169+
*/
170+
async function replaceTab(key: RouteKey, options?: App.Global.RouterPushOptions) {
171+
const oldTabId = activeTabId.value;
172+
173+
// push new route
174+
await routerPushByKey(key, options);
175+
176+
// remove old tab (exclude fixed tab)
177+
if (!isTabRetain(oldTabId)) {
178+
await removeTab(oldTabId);
179+
}
180+
}
181+
163182
/**
164183
* Switch route by tab
165184
*
@@ -282,6 +301,7 @@ export const useTabStore = defineStore(SetupStoreId.Tab, () => {
282301
removeTab,
283302
removeActiveTab,
284303
removeTabByRouteName,
304+
replaceTab,
285305
clearTabs,
286306
clearLeftTabs,
287307
clearRightTabs,

Diff for: src/typings/app.d.ts

+6
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)