修复关闭页面重新打开,无法进入动态路由问题

This commit is contained in:
ktianc 2024-01-22 22:32:14 +08:00
parent 01f1a9e88e
commit e08f0e153c
2 changed files with 11 additions and 0 deletions

View File

@ -1,3 +1,4 @@
import { hasRoute } from './router'
import router from './router' import router from './router'
import type { RouteRecordRaw } from 'vue-router' import type { RouteRecordRaw } from 'vue-router'
import { useTitle } from '@/hooks/web/useTitle' import { useTitle } from '@/hooks/web/useTitle'
@ -18,6 +19,7 @@ router.beforeEach(async (to, from, next) => {
loadStart() loadStart()
const permissionStore = usePermissionStoreWithOut() const permissionStore = usePermissionStoreWithOut()
const authStore = useAuthStoreWithOut() const authStore = useAuthStoreWithOut()
if (authStore.getToken) { if (authStore.getToken) {
if (to.path === '/login') { if (to.path === '/login') {
next({ path: '/' }) next({ path: '/' })
@ -28,6 +30,9 @@ router.beforeEach(async (to, from, next) => {
await authStore.setUserInfo() await authStore.setUserInfo()
} }
if (permissionStore.getIsAddRouters) { if (permissionStore.getIsAddRouters) {
if (!hasRoute(to.path)) {
authStore.logout('认证已过期,请重新登录!')
}
next() next()
return return
} }

View File

@ -124,6 +124,12 @@ export const resetRouter = (): void => {
}) })
} }
// 判断是否已经有某个路径的路由配置
export const hasRoute = (path: string): boolean => {
const resolvedRoute = router.resolve(path)
return resolvedRoute.matched.length > 0
}
export const setupRouter = (app: App<Element>) => { export const setupRouter = (app: App<Element>) => {
app.use(router) app.use(router)
} }