修正了路由守护

This commit is contained in:
carry 2025-02-15 16:24:03 +08:00
parent 59ef88e4a3
commit afb94ad3e1
2 changed files with 12 additions and 5 deletions

View File

@ -1,5 +1,5 @@
import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/store/userStore'
import { userStore } from '@/store/userStore'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
@ -18,20 +18,24 @@ const router = createRouter({
{
path: '/',
redirect: '/manage'
},
{
path: '/:pathMatch(.*)*',
redirect: '/login'
}
]
})
router.beforeEach((to, from, next) => {
const userStore = useUserStore()
//const userStore = useUserStore()
// 如果路由需要认证但用户未登录
if (to.meta.requiresAuth && !userStore.isLoggedIn) {
if (to.meta.requiresAuth && !userStore().isLoggedIn) {
next({ name: 'login' })
}
// 如果用户已登录但访问登录页
else if (to.name === 'login' && userStore.isLoggedIn) {
else if (to.name === 'login' && userStore().isLoggedIn) {
next({ name: 'manage' })
}
// 其他情况正常导航

View File

@ -8,7 +8,10 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true
"noUncheckedSideEffectImports": true,
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"]
}