diff --git a/src/router/index.js b/src/router/index.js index 24f2f28..9c48834 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,4 +1,5 @@ import { createRouter, createWebHistory } from 'vue-router' +import store from '../store' // 确保正确引入你的 Vuex store import HelloWorld from "../components/HelloWorld.vue"; import authenticationRoutes from './authentication' import courseManagementRoutes from './courseManagement' @@ -6,11 +7,13 @@ import meetingManagementRoutes from './meetingManagement' import newsManagementRoutes from './newsManagement' import organizationManagementRoutes from './organizationManagement' import userManagementRoutes from './userManagement' +import Home from "../components/Home.vue" // 请不要直接修改本文件,使用在router下的对应模块名称的js中引入路由的方式来代替-> const routes = [ // { path: '/', component: HelloWorld, meta: {"msg": "HeIl⚪ W0rId!"}}, + { path: '/', component: Home}, ...authenticationRoutes, ...courseManagementRoutes, ...meetingManagementRoutes, @@ -24,4 +27,16 @@ const router = createRouter({ routes }) +// 添加全局导航守卫 +router.beforeEach((to, from, next) => { + const token = store.getters['authentication/token']; + + // 如果token不存在且访问的页面不是/login或/register,则跳转到/login + if (!token && to.path !== '/login' && to.path !== '/register') { + next('/login'); + } else { + next(); + } +}); + export default router