From f3ba4c82f78bbd7f8342a76489f2942a541561bb Mon Sep 17 00:00:00 2001 From: hooray <304327508@qq.com> Date: Wed, 10 Nov 2021 10:21:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=B7=AF=E7=94=B1=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E5=89=8D=E7=BD=AE=E5=AE=88=E5=8D=AB=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/index.js | 66 ++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index 4376d25d..acb05305 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -111,32 +111,12 @@ const router = createRouter({ router.beforeEach(async(to, from, next) => { store.state.settings.enableProgress && NProgress.start() - // 已经登录,但还没根据权限动态生成并挂载路由 - if (store.getters['user/isLogin'] && !store.state.menu.isGenerate) { - let accessRoutes = [] - if (!store.state.settings.enableBackendReturnRoute) { - accessRoutes = await store.dispatch('menu/generateRoutesAtFront', { - asyncRoutes, - currentPath: to.path - }) - } else { - accessRoutes = await store.dispatch('menu/generateRoutesAtBack', { - currentPath: to.path - }) - } - accessRoutes.push(lastRoute) - let removeRoutes = [] - accessRoutes.forEach(route => { - removeRoutes.push(router.addRoute(route)) - }) - // 记录的 accessRoutes 路由数据,在登出时会使用到,不使用 router.removeRoute 是考虑配置的路由可能不一定有设置 name ,则通过调用 router.addRoute() 返回的回调进行删除 - store.commit('menu/setCurrentRemoveRoutes', removeRoutes) - next({ ...to, replace: true }) - } else { - if (store.state.menu.isGenerate && store.state.settings.menuMode !== 'single') { - store.commit('menu/setHeaderActived', to.path) - } - if (store.getters['user/isLogin']) { + // 是否已登录 + if (store.getters['user/isLogin']) { + // 是否已根据权限动态生成并挂载路由 + if (store.state.menu.isGenerate) { + // 导航栏如果不是 single 模式,则需要根据 path 定位主导航的选中状态 + store.state.settings.menuMode !== 'single' && store.commit('menu/setHeaderActived', to.path) if (to.name) { if (to.matched.length !== 0) { // 如果已登录状态下,进入登录页会强制跳转到控制台页面 @@ -168,16 +148,36 @@ router.beforeEach(async(to, from, next) => { next() } } else { - if (to.name != 'login') { - next({ - name: 'login', - query: { - redirect: to.fullPath - } + let accessRoutes = [] + if (!store.state.settings.enableBackendReturnRoute) { + accessRoutes = await store.dispatch('menu/generateRoutesAtFront', { + asyncRoutes, + currentPath: to.path }) } else { - next() + accessRoutes = await store.dispatch('menu/generateRoutesAtBack', { + currentPath: to.path + }) } + accessRoutes.push(lastRoute) + let removeRoutes = [] + accessRoutes.forEach(route => { + removeRoutes.push(router.addRoute(route)) + }) + // 记录的 accessRoutes 路由数据,在登出时会使用到,不使用 router.removeRoute 是考虑配置的路由可能不一定有设置 name ,则通过调用 router.addRoute() 返回的回调进行删除 + store.commit('menu/setCurrentRemoveRoutes', removeRoutes) + next({ ...to, replace: true }) + } + } else { + if (to.name != 'login') { + next({ + name: 'login', + query: { + redirect: to.fullPath + } + }) + } else { + next() } } })