Skip to content

Commit

Permalink
feat: routers support ignoreRoute option
Browse files Browse the repository at this point in the history
为路由配置添加`meta`.`ignoreRoute`配置,允许在`ROUTE_MAPPING`及`BACK`模式下配置纯菜单数据

fixed:
  • Loading branch information
mynetfan committed Jul 1, 2021
1 parent b5046f0 commit 72ac240
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/router/helper/menuHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export function getAllParentPath<T = Recordable>(treeData: T[], path: string) {
return (menuList || []).map((item) => item.path);
}

function isPlainPath(path: string) {
return path.indexOf(':') === -1;
}

function joinParentPath(menus: Menu[], parentPath = '') {
for (let index = 0; index < menus.length; index++) {
const menu = menus[index];
Expand All @@ -20,7 +24,7 @@ function joinParentPath(menus: Menu[], parentPath = '') {
menu.path = `${parentPath}/${menu.path}`;
}
if (menu?.children?.length) {
joinParentPath(menu.children, menu.path);
joinParentPath(menu.children, isPlainPath(menu.path) ? menu.path : parentPath);
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/router/routes/modules/demo/feat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,28 @@ const feat: AppRouteModule = {
title: t('routes.demo.feat.tab'),
carryParam: true,
},
children: [
{
path: 'testTab/id1',
name: 'TestTab1',
component: () => import('/@/views/demo/feat/tab-params/index.vue'),
meta: {
title: t('routes.demo.feat.tab1'),
carryParam: true,
ignoreRoute: true,
},
},
{
path: 'testTab/id2',
name: 'TestTab2',
component: () => import('/@/views/demo/feat/tab-params/index.vue'),
meta: {
title: t('routes.demo.feat.tab2'),
carryParam: true,
ignoreRoute: true,
},
},
],
},
],
};
Expand Down
12 changes: 12 additions & 0 deletions src/store/modules/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ export const usePermissionStore = defineStore({
return roleList.some((role) => roles.includes(role));
};

const routeRmoveIgnoreFilter = (route: AppRouteRecordRaw) => {
const { meta } = route;
const { ignoreRoute } = meta || {};
return !ignoreRoute;
};

switch (permissionMode) {
case PermissionModeEnum.ROLE:
routes = filter(asyncRoutes, routeFilter);
Expand All @@ -123,6 +129,8 @@ export const usePermissionStore = defineStore({
routes = filter(asyncRoutes, routeFilter);
routes = routes.filter(routeFilter);
const menuList = transformRouteToMenu(routes, true);
routes = filter(routes, routeRmoveIgnoreFilter);
routes = routes.filter(routeRmoveIgnoreFilter);
menuList.sort((a, b) => {
return (a.meta?.orderNo || 0) - (b.meta?.orderNo || 0);
});
Expand Down Expand Up @@ -158,6 +166,10 @@ export const usePermissionStore = defineStore({
const backMenuList = transformRouteToMenu(routeList);
this.setBackMenuList(backMenuList);

// remove meta.ignoreRoute item
routeList = filter(routeList, routeRmoveIgnoreFilter);
routeList = routeList.filter(routeRmoveIgnoreFilter);

routeList = flatMultiLevelRoutes(routeList);
routes = [PAGE_NOT_FOUND_ROUTE, ...routeList];
break;
Expand Down
1 change: 1 addition & 0 deletions types/vue-router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ declare module 'vue-router' {
// Never show in menu
hideMenu?: boolean;
isLink?: boolean;
ignoreRoute?: boolean;
}
}

0 comments on commit 72ac240

Please sign in to comment.