自定义指令封装,刷新空白问题

This commit is contained in:
Songsl 2026-04-25 17:48:05 +08:00
parent 0e480b5142
commit 96281ecf83
1 changed files with 19 additions and 11 deletions

View File

@ -69,10 +69,13 @@ const routes = [
},
],
},
// {
// path: '/:pathMatch(.*)*',
// redirect: '/user/login', // 404 重定向到登录页,避免循环
// },
{
path: '/:pathMatch(.*)*',
redirect: () => {
const token = getStorage('token')
return token ? '/dashboard' : '/user/login'
}, // 404 重定向到登录页,避免循环
},
]
const router = createRouter({
@ -87,17 +90,17 @@ router.beforeEach(async (to, _from) => {
const userStore = useUserStore()
const isLoggedIn = !!userStore.token
console.log('dynamicRoutesReady', dynamicRoutesReady)
console.log('isLoggedIn', isLoggedIn)
console.log('to.meta.requiresAuth', to.meta.requiresAuth)
console.log('to.meta.isAuthPage', to.meta.isAuthPage)
// 1. 未登录访问需要认证的页 → 登录页
if (!isLoggedIn && to.meta.requiresAuth) {
return '/user/login'
}
// 2. 已登录访问登录/注册页 → 首页
if (isLoggedIn && to.meta.isAuthPage) {
return '/'
}
// 3. ✅ 动态路由恢复(只执行一次)
// 2. ✅ 动态路由恢复(只执行一次)
if (isLoggedIn && !dynamicRoutesReady) {
const menuList = getStorage('menusInfo')
if (menuList?.length > 0) {
@ -107,10 +110,15 @@ router.beforeEach(async (to, _from) => {
routeList.forEach((route) => router.addRoute('Layout', route))
dynamicRoutesReady = true
// 返回目标路由以触发重新匹配
return { ...to, replace: true }
return to.redirectedFrom?.fullPath || true
}
}
// 3. 已登录访问登录/注册页 → 首页
if (isLoggedIn && to.meta.isAuthPage) {
return '/'
}
// 4. 其他情况正常放行
return true
})