diff --git a/src/router/index.ts b/src/router/index.ts index b041033..b81499a 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -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' }) } // 其他情况正常导航 diff --git a/tsconfig.app.json b/tsconfig.app.json index 7fb078c..19ef853 100644 --- a/tsconfig.app.json +++ b/tsconfig.app.json @@ -8,7 +8,10 @@ "noUnusedLocals": true, "noUnusedParameters": true, "noFallthroughCasesInSwitch": true, - "noUncheckedSideEffectImports": true + "noUncheckedSideEffectImports": true, + "paths": { + "@/*": ["./src/*"] + } }, "include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"] }