From ecc7a8be46475f5d7235073214857e241d4741dd Mon Sep 17 00:00:00 2001 From: RuoYi <yzz_ivy@163.com> Date: 星期三, 16 十二月 2020 20:57:48 +0800 Subject: [PATCH] 优化多级菜单之间切换无法缓存的问题 --- ruoyi-ui/src/store/modules/permission.js | 51 ++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 42 insertions(+), 9 deletions(-) diff --git a/ruoyi-ui/src/store/modules/permission.js b/ruoyi-ui/src/store/modules/permission.js index 260d8af..c2fb123 100644 --- a/ruoyi-ui/src/store/modules/permission.js +++ b/ruoyi-ui/src/store/modules/permission.js @@ -6,13 +6,17 @@ const permission = { state: { routes: [], - addRoutes: [] + addRoutes: [], + sidebarRouters: [] }, mutations: { SET_ROUTES: (state, routes) => { state.addRoutes = routes state.routes = constantRoutes.concat(routes) - } + }, + SET_SIDEBAR_ROUTERS: (state, routers) => { + state.sidebarRouters = routers + }, }, actions: { // 生成路由 @@ -20,10 +24,14 @@ return new Promise(resolve => { // 向后端请求路由数据 getRouters().then(res => { - const accessedRoutes = filterAsyncRouter(res.data) - accessedRoutes.push({ path: '*', redirect: '/404', hidden: true }) - commit('SET_ROUTES', accessedRoutes) - resolve(accessedRoutes) + const sdata = JSON.parse(JSON.stringify(res.data)) + const rdata = JSON.parse(JSON.stringify(res.data)) + const sidebarRoutes = filterAsyncRouter(sdata) + const rewriteRoutes = filterAsyncRouter(rdata, true) + rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true }) + commit('SET_ROUTES', rewriteRoutes) + commit('SET_SIDEBAR_ROUTERS', sidebarRoutes) + resolve(rewriteRoutes) }) }) } @@ -31,8 +39,11 @@ } // 遍历后台传来的路由字符串,转换为组件对象 -function filterAsyncRouter(asyncRouterMap) { +function filterAsyncRouter(asyncRouterMap, isRewrite = false) { return asyncRouterMap.filter(route => { + if (isRewrite && route.children) { + route.children = filterChildren(route.children) + } if (route.component) { // Layout ParentView 组件特殊处理 if (route.component === 'Layout') { @@ -44,14 +55,36 @@ } } if (route.children != null && route.children && route.children.length) { - route.children = filterAsyncRouter(route.children) + route.children = filterAsyncRouter(route.children, route, isRewrite) } return true }) } +function filterChildren(childrenMap) { + var children = [] + childrenMap.forEach((el, index) => { + if (el.children && el.children.length) { + if (el.component === 'ParentView') { + el.children.forEach(c => { + c.path = el.path + '/' + c.path + if (c.children && c.children.length) { + children = children.concat(filterChildren(c.children, c)) + return + } + children.push(c) + }) + childrenMap.splice(index, 1) + return + } + } + children = children.concat(el) + }) + return children +} + export const loadView = (view) => { // 路由懒加载 - return (resolve) => require([`@/views/${view}`], resolve) + return (resolve) => require([`@/views/${view}`], resolve) } export default permission -- Gitblit v1.9.2