修正了路由守护
This commit is contained in:
parent
59ef88e4a3
commit
afb94ad3e1
@ -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' })
|
||||
}
|
||||
// 其他情况正常导航
|
||||
|
@ -8,7 +8,10 @@
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noUncheckedSideEffectImports": true
|
||||
"noUncheckedSideEffectImports": true,
|
||||
"paths": {
|
||||
"@/*": ["./src/*"]
|
||||
}
|
||||
},
|
||||
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"]
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user