import { createRouter, createWebHistory } from 'vue-router' import {ElNotification} from "element-plus"; import home from "../views/Home.vue"; import Login from '../views/Login.vue' import Challenge from '../views/Challenges.vue' import Scoreboard from '../views/Scoreboard.vue' import Notification from "../views/Notification.vue"; import users from "../views/Users.vue"; const routerHistory = createWebHistory() const router = createRouter({ history: routerHistory, routes: [ { path: '/', redirect:'/home' }, { path: '/home', name:'Home', component:home, }, { path:'/login', name:'Login', component: Login, }, { path:'/challenges', name:'Challenges', component: Challenge, meta: { requireAuth: true, } }, { path:'/scoreboard', name:'Scoreboard', component: Scoreboard, }, { path:'/notification', name:'Notification', component: Notification, }, { path:'/users', name:'users', component: users, meta: { requireAuth: true, } }, { path:'/admin', name:'Admin', component: () => import('../views/admin/index.vue'), children:[ { path:'notification', name:'AdminNotification', component : () => import('../views/admin/AdminNotice.vue'), } ] }, { path:'/404', name:'NotFound', component: () => import('../views/404.vue'), }, { path: '/:pathMatch(.*)', redirect: '/404' } ] }) //路由守护,防止非登录用户访问至题目页面 router.beforeEach((to, from, next) => { if (to.matched.some(record => record.meta.requireAuth)) { if (localStorage.getItem('token')) { next() } else { ElNotification.error({ title: 'Error', message: '请先登录' }) next({ path: '/login', query: { redirect: to.fullPath } }) } } else { next() } } ) export default router