This commit is contained in:
张成
2025-10-09 18:41:16 +08:00
parent bf0d15f4aa
commit fffdb69815
2 changed files with 40 additions and 8 deletions

View File

@@ -103,7 +103,6 @@ export default {
// 动态添加路由(重要!解决登录后点击菜单空白的问题)
if (window.rootVue && window.rootVue.$router) {
// 先移除旧的主路由(如果存在)
const router = window.rootVue.$router
const routes = router.options.routes
@@ -115,7 +114,7 @@ export default {
// 添加新的主路由
router.addRoute(mainMenu)
console.log('动态路由已添加')
console.log('动态路由已添加redirect 设置为:', mainMenu.redirect)
}
}
},

View File

@@ -221,15 +221,13 @@ export default class uiTool {
component: Main,
meta: { title: '首页', notCache: true },
children: [
// 默认 home 路由,确保登录后能跳转
{
path: '/home',
name: 'home',
meta: { title: '首页', notCache: true },
component: HomePage || {
render: h => h('div', { style: { padding: '20px' } }, '欢迎使用管理系统')
}
name: '首页',
component: HomePage,
meta: { title: '首页', notCache: true }
}
]
}
@@ -269,9 +267,44 @@ export default class uiTool {
if (hasHome) {
// 如果权限路由中有 home使用权限路由的 home不添加默认首页
mainRoute.children = curRoutes
mainRoute.redirect = '/home'
} else {
// 如果权限路由中没有 home保留默认 home 并添加其他路由
mainRoute.children = [homeRoute, ...curRoutes]
mainRoute.redirect = '/home'
}
// 动态设置 redirect 到第一个有效的路由
// 优先查找首页路由,如果没有则使用第一个菜单项
const findFirstRoute = (routes) => {
if (!routes || routes.length === 0) return null
for (let route of routes) {
// 优先查找 /home 路由
if (route.path === '/home' || route.path === 'home') {
return route.path.startsWith('/') ? route.path : '/' + route.path
}
}
// 如果没有 home使用第一个页面路由
for (let route of routes) {
if (route.type !== '菜单' && route.path) {
return route.path.startsWith('/') ? route.path : '/' + route.path
}
// 如果是菜单,递归查找子路由
if (route.children && route.children.length > 0) {
const childRoute = findFirstRoute(route.children)
if (childRoute) return childRoute
}
}
return null
}
const firstRoutePath = findFirstRoute(mainRoute.children)
if (firstRoutePath) {
mainRoute.redirect = firstRoutePath
console.log('主路由 redirect 设置为:', firstRoutePath)
}
}
}