router+home

This commit is contained in:
高子兴 2024-07-06 09:22:54 +08:00
parent b4eb747b52
commit e8a0f5f13f

View File

@ -1,4 +1,5 @@
import { createRouter, createWebHistory } from 'vue-router' import { createRouter, createWebHistory } from 'vue-router'
import store from '../store' // 确保正确引入你的 Vuex store
import HelloWorld from "../components/HelloWorld.vue"; import HelloWorld from "../components/HelloWorld.vue";
import authenticationRoutes from './authentication' import authenticationRoutes from './authentication'
import courseManagementRoutes from './courseManagement' import courseManagementRoutes from './courseManagement'
@ -6,11 +7,13 @@ import meetingManagementRoutes from './meetingManagement'
import newsManagementRoutes from './newsManagement' import newsManagementRoutes from './newsManagement'
import organizationManagementRoutes from './organizationManagement' import organizationManagementRoutes from './organizationManagement'
import userManagementRoutes from './userManagement' import userManagementRoutes from './userManagement'
import Home from "../components/Home.vue"
// 请不要直接修改本文件使用在router下的对应模块名称的js中引入路由的方式来代替-> // 请不要直接修改本文件使用在router下的对应模块名称的js中引入路由的方式来代替->
const routes = [ const routes = [
// { path: '/', component: HelloWorld, meta: {"msg": "HeIl⚪ W0rId!"}}, // { path: '/', component: HelloWorld, meta: {"msg": "HeIl⚪ W0rId!"}},
{ path: '/', component: Home},
...authenticationRoutes, ...authenticationRoutes,
...courseManagementRoutes, ...courseManagementRoutes,
...meetingManagementRoutes, ...meetingManagementRoutes,
@ -24,4 +27,16 @@ const router = createRouter({
routes 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 export default router