diff --git a/src/layout/index.vue b/src/layout/index.vue index 95eec46..70e7531 100644 --- a/src/layout/index.vue +++ b/src/layout/index.vue @@ -74,7 +74,7 @@ provide('switchMenu', switchMenu) function switchMenu(index) { store.commit('menu/switchHeaderActived', index) if (store.state.settings.switchSidebarAndPageJump) { - router.push(store.getters['menu/sidebarRoutes'][0].path) + router.push(store.getters['menu/sidebarRoutesFirstDeepestPath']) } } diff --git a/src/router/index.js b/src/router/index.js index e96f46c..5cb58cc 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -2,7 +2,6 @@ import { createRouter, createWebHashHistory } from 'vue-router' import store from '@/store' import NProgress from 'nprogress' import 'nprogress/nprogress.css' // progress bar style -import path from 'path-browserify' // 固定路由 const constantRoutes = [ @@ -127,31 +126,8 @@ router.beforeEach(async(to, from, next) => { } else if (!store.state.settings.enableDashboard && to.name == 'dashboard') { // 如果未开启控制台页面,则默认进入侧边栏导航第一个模块 if (store.getters['menu/sidebarRoutes'].length > 0) { - // 此处没有使用 store.getters['menu/sidebarRoutes'][0].path 直接进行跳转是因为会有个隐性 bug ,即当父级路由如果设置了 redirect ,而 redirect 对应的嵌套子路由由于没有权限导致没有被注册,此时则会无限进入 404 页面 - let getDeepestPath = (routes, rootPath = '') => { - let retnPath - if (routes.children) { - if ( - routes.children.some(item => { - return item.meta.sidebar != false - }) - ) { - for (let i = 0; i < routes.children.length; i++) { - if (routes.children[i].meta.sidebar != false) { - retnPath = getDeepestPath(routes.children[i], path.resolve(rootPath, routes.path)) - break - } - } - } else { - retnPath = getDeepestPath(routes.children[0], path.resolve(rootPath, routes.path)) - } - } else { - retnPath = path.resolve(rootPath, routes.path) - } - return retnPath - } next({ - path: getDeepestPath(store.getters['menu/sidebarRoutes'][0]), + path: store.getters['menu/sidebarRoutesFirstDeepestPath'], replace: true }) } else { diff --git a/src/store/modules/menu.js b/src/store/modules/menu.js index 2477f30..ff2559e 100644 --- a/src/store/modules/menu.js +++ b/src/store/modules/menu.js @@ -114,6 +114,29 @@ function flatAsyncRoutes(routes, breadcrumb, baseUrl = '') { return res } +function getDeepestPath(routes, rootPath = '') { + let retnPath + if (routes.children) { + if ( + routes.children.some(item => { + return item.meta.sidebar != false + }) + ) { + for (let i = 0; i < routes.children.length; i++) { + if (routes.children[i].meta.sidebar != false) { + retnPath = getDeepestPath(routes.children[i], path.resolve(rootPath, routes.path)) + break + } + } + } else { + retnPath = getDeepestPath(routes.children[0], path.resolve(rootPath, routes.path)) + } + } else { + retnPath = path.resolve(rootPath, routes.path) + } + return retnPath +} + const state = () => ({ isGenerate: false, routes: [], @@ -138,6 +161,9 @@ const getters = { }, sidebarRoutes: (state, getters) => { return getters.routes.length > 0 ? getters.routes[state.headerActived].children : [] + }, + sidebarRoutesFirstDeepestPath: (state, getters) => { + return getDeepestPath(getters.sidebarRoutes[0]) } }