|
@@ -1,48 +1,81 @@
|
|
|
-import { createRouter, createWebHistory, } from 'vue-router'
|
|
|
-import Login from '../pages/login.vue';
|
|
|
+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"),
|
|
|
+ },
|
|
|
+];
|
|
|
|
|
|
-export const routes = [
|
|
|
- {
|
|
|
- path: '/',
|
|
|
- redirect: '/login'
|
|
|
- },
|
|
|
- {
|
|
|
- name: 'login',
|
|
|
- path: '/login',
|
|
|
- component: Login
|
|
|
- },
|
|
|
- {
|
|
|
- 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,
|
|
|
-})
|
|
|
+ scrollBehavior: () => ({ left: 0, top: 0 }),
|
|
|
+ history: createWebHistory(),
|
|
|
+ routes,
|
|
|
+});
|
|
|
router.beforeEach((_to, _from, next) => {
|
|
|
- next()
|
|
|
-})
|
|
|
-export default router
|
|
|
+ const routerList = useStore().routers;
|
|
|
+ const routers = router.getRoutes();
|
|
|
+ console.log(routerList, routers);
|
|
|
+ const { setAsyncRoutesMark, asyncRoutesMark } = useStore();
|
|
|
+ const token = true;
|
|
|
+ if (_to.path == "/login") {
|
|
|
+ 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.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;
|