| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- import { RouteRecordRaw, createRouter, createWebHistory } from "vue-router";
- import { useStore } from "@/store/index";
- export const routes: RouteRecordRaw[] = [
- {
- path: "/",
- redirect: "/login",
- },
- {
- name: "login",
- path: "/login",
- component: () => import("../pages/login.vue"),
- },
- {
- name: "register",
- path: "/register",
- component: () => import("../pages/register.vue"),
- },
- {
- name: "home",
- path: "/home",
- component: () => import("../pages/home.vue"),
- children: [
- // {
- // name: 'thread',
- // path: '/thread',
- // component: () => import("../pages/thread/thread.vue")
- // },
- ],
- },
- {
- name: "test",
- path: "/test",
- component: () => import("../pages/test/index.vue"),
- },
- {
- name: "testEcharts",
- path: "/testEcharts",
- component: () => import("../pages/test/echarts.vue"),
- },
- ];
- const router = createRouter({
- scrollBehavior: () => ({ left: 0, top: 0 }),
- history: createWebHistory(),
- routes,
- });
- router.beforeEach((to, _from, next) => {
- const routerList = useStore().routers;
- const routers = router.getRoutes();
- //console.log(routerList, routers);
- const { setAsyncRoutesMark, asyncRoutesMark, getToken } = useStore();
- const token = getToken;
- const skipPath = ["/login", "/register", "/test", "/testEcharts"];
- //console.log(token, '<==== token')
- if (skipPath.includes(to.path)) {
- next();
- } else {
- if (token && routerList && routerList.length > 0) {
- if (asyncRoutesMark) {
- next();
- } else {
- setAsyncRoutesMark(true);
- const newRouters: any = routers;
- const addNewRouter = newRouters.find(
- (item: any) => item.path == "/home"
- );
- routerList.forEach((item: any) => {
- let filePath = item.path.split("/")[1];
- if (item.children && item.children.length > 0) {
- item.children.forEach((child: any) => {
- let childFilePath = child.path.split("/")[1];
- addNewRouter?.children.push({
- path: child.path,
- name: child.name,
- meta: {},
- component: () =>
- import(`@/pages/${filePath}/${childFilePath}/index.vue`),
- });
- });
- } else {
- addNewRouter?.children.push({
- path: item.path,
- name: item.name,
- meta: {},
- component: () => import(`@/pages/${filePath}/index.vue`),
- });
- }
- });
- router.addRoute(addNewRouter);
- next({ ...to, replace: true });
- }
- } else {
- //console.log("无登录信息,跳转到登录页");
- next(`/login`);
- }
- }
- //console.log(routerList);
- });
- export default router;
|