Skip to content

feat(projects): support replaceTab. #713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/hooks/common/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ export function useRouterPush(inSetup = true) {

const routerBack = router.back;

interface RouterPushOptions {
query?: Record<string, string>;
params?: Record<string, string>;
}

async function routerPushByKey(key: RouteKey, options?: RouterPushOptions) {
async function routerPushByKey(key: RouteKey, options?: App.Global.RouterPushOptions) {
const { query, params } = options || {};

const routeLocation: RouteLocationRaw = {
Expand Down Expand Up @@ -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
}
Expand Down
20 changes: 20 additions & 0 deletions src/store/modules/tab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -282,6 +301,7 @@ export const useTabStore = defineStore(SetupStoreId.Tab, () => {
removeTab,
removeActiveTab,
removeTabByRouteName,
replaceTab,
clearTabs,
clearLeftTabs,
clearRightTabs,
Expand Down
6 changes: 6 additions & 0 deletions src/typings/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>;
params?: Record<string, string>;
};

/** The global header props */
interface HeaderProps {
/** Whether to show the logo */
Expand Down