修复开启切换侧边栏同时跳转页面的配置项后无法跳转的问题

This commit is contained in:
hooray 2021-11-03 10:48:43 +08:00
parent 5fbb0130cf
commit 56d67ddd6b
3 changed files with 28 additions and 26 deletions

View File

@ -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'])
}
}
</script>

View File

@ -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 {

View File

@ -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])
}
}