fix(router): 修复动态路由加载和错误处理机制
- 将动态路由添加到路由器配置中 - 添加路由获取失败时的错误处理和404页面跳转 - 优化路由加载失败时的日志记录和错误提示 - 修复登出后的重定向路径为登录页面 - 统一错误消息显示格式
This commit is contained in:
@@ -56,9 +56,10 @@ router.beforeEach(async (to, from) => {
|
||||
})
|
||||
return { ...to, replace: true }
|
||||
} catch (err) {
|
||||
console.error('路由加载失败:', err)
|
||||
await useUserStore().logOut()
|
||||
ElMessage.error(err)
|
||||
return { path: '/' }
|
||||
ElMessage.error(err.message || '登录已过期')
|
||||
return { path: '/login' }
|
||||
}
|
||||
}
|
||||
return true
|
||||
|
||||
@@ -456,7 +456,7 @@ export const dynamicRoutes = [
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes: constantRoutes,
|
||||
routes: [...constantRoutes, ...dynamicRoutes],
|
||||
scrollBehavior(to, from, savedPosition) {
|
||||
if (savedPosition) {
|
||||
return savedPosition
|
||||
|
||||
@@ -50,6 +50,11 @@ const usePermissionStore = defineStore(
|
||||
this.setDefaultRoutes(sidebarRoutes)
|
||||
this.setTopbarRoutes(defaultRoutes)
|
||||
resolve(rewriteRoutes)
|
||||
}).catch(err => {
|
||||
console.error('获取路由失败:', err)
|
||||
addNotFoundRoute()
|
||||
this.setRoutes([])
|
||||
resolve([])
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user