优化了路由配置
This commit is contained in:
parent
85dcc74e11
commit
b38c6a7d47
@ -5,6 +5,14 @@ import { useUserStore } from '../stores/user'
|
|||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
history: createWebHistory(),
|
history: createWebHistory(),
|
||||||
routes: [
|
routes: [
|
||||||
|
{
|
||||||
|
path: '/',
|
||||||
|
redirect: to => {
|
||||||
|
// 如果已登录,重定向到后台首页,否则重定向到登录页
|
||||||
|
const userStore = useUserStore()
|
||||||
|
return userStore.isLoggedIn ? '/dashboard' : '/login'
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
path: '/login',
|
path: '/login',
|
||||||
name: 'Login',
|
name: 'Login',
|
||||||
@ -115,7 +123,7 @@ router.beforeEach((to, from, next) => {
|
|||||||
if (to.matched.some(record => record.meta.requiresAuth) && !userStore.isLoggedIn) {
|
if (to.matched.some(record => record.meta.requiresAuth) && !userStore.isLoggedIn) {
|
||||||
next({
|
next({
|
||||||
path: '/login',
|
path: '/login',
|
||||||
query: { redirect: to.fullPath } // 保存原目标路径
|
query: { redirect: to.fullPath }
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
next()
|
next()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user