import { createRouter, createWebHistory } from "vue-router"; import LayOut from "@/layout/index.vue"; import { sessionStorageTool } from "@/utils/storage"; import { SESSION_STORAGE_KEYS } from "@/constants/storage"; const router = createRouter({ routes: [ { path: "/", component: LayOut, children: [ { path: "exam", name: "exam", component: () => import("@/pages/exam-manage/index.vue"), }, { path: "subjects", name: "subjects", component: () => import("@/pages/subjects-manage/index.vue"), }, { path: "user", name: "user", component: () => import("@/pages/user-manage/index.vue"), }, { path: "school", name: "school", component: () => import("@/pages/school-manage/index.vue"), }, ], }, { path: "/", name: "login", component: () => import("@/pages/login/index.vue"), }, ], history: createWebHistory(), }); router.beforeEach((to, from, next) => { if (to.name === "login") { sessionStorageTool.remove(SESSION_STORAGE_KEYS.LOGIN_RESULT); return next(); } if (!sessionStorageTool.get(SESSION_STORAGE_KEYS.LOGIN_RESULT)) { return next({ name: "login" }); } next(); }); export default router;