1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- 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 } = useStore();
- const token = true;
- const skipPath = ['/login', '/register', '/test', '/testEcharts']
- if (skipPath.includes(_to.path)) {
- next();
- } else {
- if (token && routerList && routerList.length > 0) {
- if (asyncRoutesMark) {
- console.log(routers, '<=== 以及有了')
- next();
- } else {
- setAsyncRoutesMark(true)
- const newRouters: any = routers
- const addNewRouter = newRouters.find((item: any) => item.name == 'home')
- routerList.forEach((item: any) => {
- addNewRouter?.children.push({
- path: item.path,
- name: item.name,
- meta: {},
- component: () => import(`@/pages/${item.name}/index.vue`),
- })
- })
- router.addRoute(addNewRouter);
- next({ ..._to, replace: true });
- }
- } else {
- console.log("无登录信息,跳转到登录页");
- next(`/login`);
- }
- }
- console.log(routerList);
- });
- export default router;
|