Commit 72ac240f2858cd74cb62b7647ca89d63bb71d247
1 parent
b5046f07
feat: routers support `ignoreRoute` option
为路由配置添加`meta`.`ignoreRoute`配置,允许在`ROUTE_MAPPING`及`BACK`模式下配置纯菜单数据 fixed:
Showing
4 changed files
with
40 additions
and
1 deletions
src/router/helper/menuHelper.ts
@@ -9,6 +9,10 @@ export function getAllParentPath<T = Recordable>(treeData: T[], path: string) { | @@ -9,6 +9,10 @@ export function getAllParentPath<T = Recordable>(treeData: T[], path: string) { | ||
9 | return (menuList || []).map((item) => item.path); | 9 | return (menuList || []).map((item) => item.path); |
10 | } | 10 | } |
11 | 11 | ||
12 | +function isPlainPath(path: string) { | ||
13 | + return path.indexOf(':') === -1; | ||
14 | +} | ||
15 | + | ||
12 | function joinParentPath(menus: Menu[], parentPath = '') { | 16 | function joinParentPath(menus: Menu[], parentPath = '') { |
13 | for (let index = 0; index < menus.length; index++) { | 17 | for (let index = 0; index < menus.length; index++) { |
14 | const menu = menus[index]; | 18 | const menu = menus[index]; |
@@ -20,7 +24,7 @@ function joinParentPath(menus: Menu[], parentPath = '') { | @@ -20,7 +24,7 @@ function joinParentPath(menus: Menu[], parentPath = '') { | ||
20 | menu.path = `${parentPath}/${menu.path}`; | 24 | menu.path = `${parentPath}/${menu.path}`; |
21 | } | 25 | } |
22 | if (menu?.children?.length) { | 26 | if (menu?.children?.length) { |
23 | - joinParentPath(menu.children, menu.path); | 27 | + joinParentPath(menu.children, isPlainPath(menu.path) ? menu.path : parentPath); |
24 | } | 28 | } |
25 | } | 29 | } |
26 | } | 30 | } |
src/router/routes/modules/demo/feat.ts
@@ -241,6 +241,28 @@ const feat: AppRouteModule = { | @@ -241,6 +241,28 @@ const feat: AppRouteModule = { | ||
241 | title: t('routes.demo.feat.tab'), | 241 | title: t('routes.demo.feat.tab'), |
242 | carryParam: true, | 242 | carryParam: true, |
243 | }, | 243 | }, |
244 | + children: [ | ||
245 | + { | ||
246 | + path: 'testTab/id1', | ||
247 | + name: 'TestTab1', | ||
248 | + component: () => import('/@/views/demo/feat/tab-params/index.vue'), | ||
249 | + meta: { | ||
250 | + title: t('routes.demo.feat.tab1'), | ||
251 | + carryParam: true, | ||
252 | + ignoreRoute: true, | ||
253 | + }, | ||
254 | + }, | ||
255 | + { | ||
256 | + path: 'testTab/id2', | ||
257 | + name: 'TestTab2', | ||
258 | + component: () => import('/@/views/demo/feat/tab-params/index.vue'), | ||
259 | + meta: { | ||
260 | + title: t('routes.demo.feat.tab2'), | ||
261 | + carryParam: true, | ||
262 | + ignoreRoute: true, | ||
263 | + }, | ||
264 | + }, | ||
265 | + ], | ||
244 | }, | 266 | }, |
245 | ], | 267 | ], |
246 | }; | 268 | }; |
src/store/modules/permission.ts
@@ -111,6 +111,12 @@ export const usePermissionStore = defineStore({ | @@ -111,6 +111,12 @@ export const usePermissionStore = defineStore({ | ||
111 | return roleList.some((role) => roles.includes(role)); | 111 | return roleList.some((role) => roles.includes(role)); |
112 | }; | 112 | }; |
113 | 113 | ||
114 | + const routeRmoveIgnoreFilter = (route: AppRouteRecordRaw) => { | ||
115 | + const { meta } = route; | ||
116 | + const { ignoreRoute } = meta || {}; | ||
117 | + return !ignoreRoute; | ||
118 | + }; | ||
119 | + | ||
114 | switch (permissionMode) { | 120 | switch (permissionMode) { |
115 | case PermissionModeEnum.ROLE: | 121 | case PermissionModeEnum.ROLE: |
116 | routes = filter(asyncRoutes, routeFilter); | 122 | routes = filter(asyncRoutes, routeFilter); |
@@ -123,6 +129,8 @@ export const usePermissionStore = defineStore({ | @@ -123,6 +129,8 @@ export const usePermissionStore = defineStore({ | ||
123 | routes = filter(asyncRoutes, routeFilter); | 129 | routes = filter(asyncRoutes, routeFilter); |
124 | routes = routes.filter(routeFilter); | 130 | routes = routes.filter(routeFilter); |
125 | const menuList = transformRouteToMenu(routes, true); | 131 | const menuList = transformRouteToMenu(routes, true); |
132 | + routes = filter(routes, routeRmoveIgnoreFilter); | ||
133 | + routes = routes.filter(routeRmoveIgnoreFilter); | ||
126 | menuList.sort((a, b) => { | 134 | menuList.sort((a, b) => { |
127 | return (a.meta?.orderNo || 0) - (b.meta?.orderNo || 0); | 135 | return (a.meta?.orderNo || 0) - (b.meta?.orderNo || 0); |
128 | }); | 136 | }); |
@@ -158,6 +166,10 @@ export const usePermissionStore = defineStore({ | @@ -158,6 +166,10 @@ export const usePermissionStore = defineStore({ | ||
158 | const backMenuList = transformRouteToMenu(routeList); | 166 | const backMenuList = transformRouteToMenu(routeList); |
159 | this.setBackMenuList(backMenuList); | 167 | this.setBackMenuList(backMenuList); |
160 | 168 | ||
169 | + // remove meta.ignoreRoute item | ||
170 | + routeList = filter(routeList, routeRmoveIgnoreFilter); | ||
171 | + routeList = routeList.filter(routeRmoveIgnoreFilter); | ||
172 | + | ||
161 | routeList = flatMultiLevelRoutes(routeList); | 173 | routeList = flatMultiLevelRoutes(routeList); |
162 | routes = [PAGE_NOT_FOUND_ROUTE, ...routeList]; | 174 | routes = [PAGE_NOT_FOUND_ROUTE, ...routeList]; |
163 | break; | 175 | break; |
types/vue-router.d.ts