web/src/router/index.ts
@@ -1,4 +1,5 @@ import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router' import { isLoggedIn } from '@/utils/auth' const routes: RouteRecordRaw[] = [ { @@ -105,4 +106,28 @@ routes }) // 路由守卫 router.beforeEach((to, from, next) => { // 如果是登录页面,直接放行 if (to.path === '/login') { // 如果已经登录,重定向到首页 if (isLoggedIn()) { next('/') } else { next() } return } // 检查是否已登录 if (!isLoggedIn()) { // 未登录,重定向到登录页 next('/login') return } // 已登录,正常访问 next() }) export default router