Skip to content

Commit 874aaca

Browse files
committed
feat(projects): Add route meta parameter:fixedQuery
1 parent 7a2f54b commit 874aaca

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/layouts/modules/global-menu/base-menu.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ function updateExpandedKeys() {
5757
}
5858
5959
function handleClickMenu(key: RouteKey) {
60+
const meta = routeStore.getSelectedMenuMetaByKey(key);
61+
if (meta?.fixedQuery) {
62+
routerPushByKey(key, {
63+
query: meta.fixedQuery
64+
});
65+
return;
66+
}
6067
routerPushByKey(key);
6168
}
6269

src/store/modules/route/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { computed, ref } from 'vue';
2-
import type { RouteRecordRaw } from 'vue-router';
2+
import type { RouteMeta, RouteRecordRaw } from 'vue-router';
33
import { defineStore } from 'pinia';
44
import { useBoolean } from '@sa/hooks';
55
import type { CustomRoute, ElegantConstRoute, LastLevelRouteKey, RouteKey, RouteMap } from '@elegant-router/types';
@@ -273,6 +273,16 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
273273
return getSelectedMenuKeyPathByKey(selectedKey, menus.value);
274274
}
275275

276+
/**
277+
* Get selected menu meta by key
278+
*
279+
* @param selectedKey Selected menu key
280+
*/
281+
function getSelectedMenuMetaByKey(selectedKey: string): RouteMeta | null {
282+
// The routes in router.options.routes are static, you need to use router.getRoutes() to get all the routes.
283+
return router.getRoutes().find(route => route.name === selectedKey)?.meta || null;
284+
}
285+
276286
return {
277287
resetStore,
278288
routeHome,
@@ -287,6 +297,7 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
287297
isInitAuthRoute,
288298
setIsInitAuthRoute,
289299
getIsAuthRouteExist,
290-
getSelectedMenuKeyPath
300+
getSelectedMenuKeyPath,
301+
getSelectedMenuMetaByKey
291302
};
292303
});

src/typings/router.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,7 @@ declare module 'vue-router' {
5959
multiTab?: boolean;
6060
/** If set, the route will be fixed in tabs, and the value is the order of fixed tabs */
6161
fixedIndexInTab?: number;
62+
/** Fixed query parameters that are automatically carried when entering the route */
63+
fixedQuery?: Record<string, string>;
6264
}
6365
}

0 commit comments

Comments
 (0)