Skip to content

Commit

Permalink
feat(projects): support route query in menu
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Mar 11, 2024
1 parent 64b272c commit 063f563
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/layouts/modules/global-menu/base-menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ const selectedBgColor = computed(() => {
});
function handleClickMenu(menuInfo: MenuInfo) {
const routeKey = menuInfo.key as RouteKey;
const key = menuInfo.key as RouteKey;
routerPushByKey(routeKey);
const { query } = routeStore.getSelectedMenuMetaByKey(key) || {};
routerPushByKey(key, { query });
}
</script>

Expand Down
15 changes: 14 additions & 1 deletion src/store/modules/route/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
return getSelectedMenuKeyPathByKey(selectedKey, menus.value);
}

/**
* Get selected menu meta by key
*
* @param selectedKey Selected menu key
*/
function getSelectedMenuMetaByKey(selectedKey: string) {
// The routes in router.options.routes are static, you need to use router.getRoutes() to get all the routes.
const allRoutes = router.getRoutes();

return allRoutes.find(route => route.name === selectedKey)?.meta || null;
}

return {
resetStore,
routeHome,
Expand All @@ -284,6 +296,7 @@ export const useRouteStore = defineStore(SetupStoreId.Route, () => {
isInitAuthRoute,
setIsInitAuthRoute,
getIsAuthRouteExist,
getSelectedMenuKeyPath
getSelectedMenuKeyPath,
getSelectedMenuMetaByKey
};
});
2 changes: 2 additions & 0 deletions src/typings/router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,7 @@ declare module 'vue-router' {
multiTab?: boolean;
/** If set, the route will be fixed in tabs, and the value is the order of fixed tabs */
fixedIndexInTab?: number;
/** if set query parameters, it will be automatically carried when entering the route */
query?: Record<string, string>;
}
}

0 comments on commit 063f563

Please sign in to comment.