Commit 8252b9ebc4b09b0672e7b83ad5ca5aa3323da452

Authored by 谭毅彬
1 parent e777e38a

三级路由缓存及路由问题解决

Signed-off-by: TanYibin <5491541@qq.com>
ant-design-vue-jeecg/src/components/menu/index.js
... ... @@ -74,20 +74,22 @@ export default {
74 74 }
75 75 },
76 76 updateMenu() {
77   - const routes = this.$route.matched.concat()
78   - const {hidden} = this.$route.meta
  77 + // 解决三级菜单缓存及路由问题 start
  78 + const routes = this.$route.meta['matched'] // 新修改,展开菜单问题
  79 + const { hidden, selectedKeys } = this.$route.meta
79 80 if (routes.length >= 3 && hidden) {
80   - routes.pop()
81   - this.selectedKeys = [routes[routes.length - 1].path]
  81 + routes.pop()
  82 + this.selectedKeys = selectedKeys
82 83 } else {
83   - this.selectedKeys = [routes.pop().path]
  84 + this.selectedKeys = [routes.pop().path]
84 85 }
85 86 let openKeys = []
86 87 if (this.mode === 'inline') {
87   - routes.forEach(item => {
88   - openKeys.push(item.path)
89   - })
  88 + routes.forEach(item => {
  89 + openKeys.push(item.path)
  90 + })
90 91 }
  92 + // 解决三级菜单缓存及路由问题 end
91 93  
92 94 // update-begin-author:sunjianlei date:20210409 for: 修复动态功能测试菜单、带参数菜单标题错误、展开错误的问题
93 95 // 包含冒号的是动态菜单
... ...
ant-design-vue-jeecg/src/permission.js
... ... @@ -3,27 +3,29 @@ import router from &#39;./router&#39;
3 3 import store from './store'
4 4 import NProgress from 'nprogress' // progress bar
5 5 import 'nprogress/nprogress.css' // progress bar style
6   -import {ACCESS_TOKEN, INDEX_MAIN_PAGE_PATH, OAUTH2_LOGIN_PAGE_PATH} from '@/store/mutation-types'
7   -import {generateIndexRouter, isOAuth2AppEnv} from '@/utils/util'
  6 +import { ACCESS_TOKEN, INDEX_MAIN_PAGE_PATH, OAUTH2_LOGIN_PAGE_PATH } from '@/store/mutation-types'
  7 +import { generateIndexRouter, isOAuth2AppEnv } from '@/utils/util'
8 8  
9   -NProgress.configure({showSpinner: false}) // NProgress Configuration
  9 +NProgress.configure({ showSpinner: false }) // NProgress Configuration
10 10  
11   -const whiteList = ['/user/login', '/user/register', '/user/register-result', '/user/alteration','/user/systemTokenModal'] // no redirect whitelist
  11 +const whiteList = ['/user/login', '/user/register', '/user/register-result', '/user/alteration', '/user/systemTokenModal'] // no redirect whitelist
12 12 whiteList.push(OAUTH2_LOGIN_PAGE_PATH)
13 13  
14 14 router.beforeEach((to, from, next) => {
15   - //update-begin---author:scott ---date:2022-10-13 for:[jeecg-boot/issues/4091]多级路由缓存问题 #4091-----------
16   - //解决三级菜单无法缓存问题
17   - //参考: https://blog.csdn.net/qq_37322135/article/details/126013301
18   - //参考: https://blog.csdn.net/cwin8951/article/details/106644118
19   - if (to.matched && to.matched.length > 3) {
20   - to.matched.splice(2, to.matched.length - 3)
  15 + // 解决三级菜单缓存及路由问题 start
  16 + to.meta['matched'] = to.matched.filter((e) => {
  17 + return e
  18 + })
  19 + if (!to.meta.isAlwaysShow && to.matched && to.matched.length > 3) {
  20 + to.matched.splice(2, to.matched.length - 3)
21 21 }
  22 + // 解决三级菜单缓存及路由问题 end
  23 +
22 24 NProgress.start() // start progress bar
23 25 if (Vue.ls.get(ACCESS_TOKEN)) {
24 26 /* has token */
25 27 if (to.path === '/user/login' || to.path === OAUTH2_LOGIN_PAGE_PATH) {
26   - next({path: INDEX_MAIN_PAGE_PATH})
  28 + next({ path: INDEX_MAIN_PAGE_PATH })
27 29 NProgress.done()
28 30 } else {
29 31 if (store.getters.permissionList.length === 0) {
... ... @@ -36,17 +38,17 @@ router.beforeEach((to, from, next) =&gt; {
36 38 let constRoutes = [];
37 39 constRoutes = generateIndexRouter(menuData);
38 40 // 添加主界面路由
39   - store.dispatch('UpdateAppRouter', {constRoutes}).then(() => {
  41 + store.dispatch('UpdateAppRouter', { constRoutes }).then(() => {
40 42 // 根据roles权限生成可访问的路由表
41 43 // 动态添加可访问路由表
42 44 router.addRoutes(store.getters.addRouters)
43 45 const redirect = decodeURIComponent(from.query.redirect || to.path)
44 46 if (to.path === redirect) {
45 47 // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
46   - next({...to, replace: true})
  48 + next({ ...to, replace: true })
47 49 } else {
48 50 // 跳转到目的路由
49   - next({path: redirect})
  51 + next({ path: redirect })
50 52 }
51 53 })
52 54 })
... ... @@ -56,7 +58,7 @@ router.beforeEach((to, from, next) =&gt; {
56 58 description: '请求用户信息失败,请重试!'
57 59 })*/
58 60 store.dispatch('Logout').then(() => {
59   - next({path: '/user/login', query: {redirect: to.fullPath}})
  61 + next({ path: '/user/login', query: { redirect: to.fullPath } })
60 62 })
61 63 })
62 64 } else {
... ... @@ -67,7 +69,7 @@ router.beforeEach((to, from, next) =&gt; {
67 69 if (whiteList.indexOf(to.path) !== -1) {
68 70 // 在免登录白名单,如果进入的页面是login页面并且当前是OAuth2app环境,就进入OAuth2登录页面
69 71 if (to.path === '/user/login' && isOAuth2AppEnv()) {
70   - next({path: OAUTH2_LOGIN_PAGE_PATH})
  72 + next({ path: OAUTH2_LOGIN_PAGE_PATH })
71 73 } else {
72 74 // 在免登录白名单,直接进入
73 75 next()
... ... @@ -76,7 +78,7 @@ router.beforeEach((to, from, next) =&gt; {
76 78 } else {
77 79 // 如果当前是在OAuth2APP环境,就跳转到OAuth2登录页面
78 80 let path = isOAuth2AppEnv() ? OAUTH2_LOGIN_PAGE_PATH : '/user/login'
79   - next({path: path, query: {redirect: to.fullPath}})
  81 + next({ path: path, query: { redirect: to.fullPath } })
80 82 NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
81 83 }
82 84 }
... ...