2024-11-19 13:46:35 +08:00
|
|
|
|
import type { RouteRecordRaw } from "vue-router"
|
2024-11-26 14:45:42 +08:00
|
|
|
|
import { routerConfig } from "@/router/config"
|
|
|
|
|
import { registerNavigationGuard } from "@/router/guard"
|
2024-11-19 13:46:35 +08:00
|
|
|
|
import { createRouter } from "vue-router"
|
2024-11-26 14:45:42 +08:00
|
|
|
|
import { flatMultiLevelRoutes } from "./helper"
|
2022-10-18 15:07:42 +08:00
|
|
|
|
|
2023-07-06 13:14:44 +08:00
|
|
|
|
const Layouts = () => import("@/layouts/index.vue")
|
2022-04-21 18:20:39 +08:00
|
|
|
|
|
2023-08-15 13:36:54 +08:00
|
|
|
|
/**
|
2024-11-25 14:21:18 +08:00
|
|
|
|
* @name 常驻路由
|
2024-11-27 15:23:28 +08:00
|
|
|
|
* @description 除了 redirect/403/404/login 等隐藏页面,其他页面建议设置唯一的 Name 属性
|
2023-08-15 13:36:54 +08:00
|
|
|
|
*/
|
2022-08-23 18:09:13 +08:00
|
|
|
|
export const constantRoutes: RouteRecordRaw[] = [
|
2022-04-21 18:20:39 +08:00
|
|
|
|
{
|
2022-04-22 01:16:02 +08:00
|
|
|
|
path: "/redirect",
|
2023-07-06 13:14:44 +08:00
|
|
|
|
component: Layouts,
|
2022-04-21 18:20:39 +08:00
|
|
|
|
meta: {
|
|
|
|
|
hidden: true
|
|
|
|
|
},
|
|
|
|
|
children: [
|
|
|
|
|
{
|
2024-03-03 23:00:17 +08:00
|
|
|
|
path: ":path(.*)",
|
2024-11-22 14:22:57 +08:00
|
|
|
|
component: () => import("@/pages/redirect/index.vue")
|
2022-04-21 18:20:39 +08:00
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
2022-09-29 20:34:25 +08:00
|
|
|
|
{
|
|
|
|
|
path: "/403",
|
2024-11-27 14:12:44 +08:00
|
|
|
|
component: () => import("@/pages/error/403.vue"),
|
2022-09-29 20:34:25 +08:00
|
|
|
|
meta: {
|
|
|
|
|
hidden: true
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: "/404",
|
2024-11-27 14:12:44 +08:00
|
|
|
|
component: () => import("@/pages/error/404.vue"),
|
2022-09-29 20:34:25 +08:00
|
|
|
|
meta: {
|
|
|
|
|
hidden: true
|
|
|
|
|
},
|
|
|
|
|
alias: "/:pathMatch(.*)*"
|
|
|
|
|
},
|
2022-04-21 18:20:39 +08:00
|
|
|
|
{
|
2022-04-22 01:16:02 +08:00
|
|
|
|
path: "/login",
|
2024-11-22 14:22:57 +08:00
|
|
|
|
component: () => import("@/pages/login/index.vue"),
|
2022-04-21 18:20:39 +08:00
|
|
|
|
meta: {
|
|
|
|
|
hidden: true
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
2022-04-22 01:16:02 +08:00
|
|
|
|
path: "/",
|
2023-07-06 13:14:44 +08:00
|
|
|
|
component: Layouts,
|
2022-04-22 01:16:02 +08:00
|
|
|
|
redirect: "/dashboard",
|
2022-04-21 18:20:39 +08:00
|
|
|
|
children: [
|
|
|
|
|
{
|
2022-04-22 01:16:02 +08:00
|
|
|
|
path: "dashboard",
|
2024-11-22 14:22:57 +08:00
|
|
|
|
component: () => import("@/pages/dashboard/index.vue"),
|
2022-04-22 01:16:02 +08:00
|
|
|
|
name: "Dashboard",
|
2022-04-21 18:20:39 +08:00
|
|
|
|
meta: {
|
2022-04-22 01:16:02 +08:00
|
|
|
|
title: "首页",
|
2022-09-30 17:41:47 +08:00
|
|
|
|
svgIcon: "dashboard",
|
2022-04-21 18:20:39 +08:00
|
|
|
|
affix: true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
2022-04-28 15:48:19 +08:00
|
|
|
|
},
|
2022-05-12 19:07:54 +08:00
|
|
|
|
{
|
2024-11-27 15:23:28 +08:00
|
|
|
|
path: "/demo",
|
2023-07-06 13:14:44 +08:00
|
|
|
|
component: Layouts,
|
2024-11-27 15:23:28 +08:00
|
|
|
|
redirect: "/demo/unocss",
|
|
|
|
|
name: "Demo",
|
2023-08-29 19:12:56 +08:00
|
|
|
|
meta: {
|
2024-11-27 19:13:47 +08:00
|
|
|
|
title: "示例集合",
|
2024-11-27 15:23:28 +08:00
|
|
|
|
elIcon: "DataBoard"
|
2023-08-29 19:12:56 +08:00
|
|
|
|
},
|
2022-04-28 15:48:19 +08:00
|
|
|
|
children: [
|
|
|
|
|
{
|
2024-11-27 15:23:28 +08:00
|
|
|
|
path: "unocss",
|
|
|
|
|
component: () => import("@/pages/demo/unocss/index.vue"),
|
|
|
|
|
name: "UnoCSS",
|
2023-08-29 19:12:56 +08:00
|
|
|
|
meta: {
|
2024-11-27 15:23:28 +08:00
|
|
|
|
title: "UnoCSS"
|
2023-08-29 19:12:56 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
2022-10-20 20:58:57 +08:00
|
|
|
|
{
|
|
|
|
|
path: "element-plus",
|
2024-11-27 15:23:28 +08:00
|
|
|
|
component: () => import("@/pages/demo/element-plus/index.vue"),
|
2022-10-20 20:58:57 +08:00
|
|
|
|
name: "ElementPlus",
|
|
|
|
|
meta: {
|
2023-02-22 15:53:04 +08:00
|
|
|
|
title: "Element Plus",
|
|
|
|
|
keepAlive: true
|
2022-10-20 20:58:57 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: "vxe-table",
|
2024-11-27 15:23:28 +08:00
|
|
|
|
component: () => import("@/pages/demo/vxe-table/index.vue"),
|
2022-10-20 20:58:57 +08:00
|
|
|
|
name: "VxeTable",
|
|
|
|
|
meta: {
|
2023-02-22 15:53:04 +08:00
|
|
|
|
title: "Vxe Table",
|
|
|
|
|
keepAlive: true
|
2022-10-20 20:58:57 +08:00
|
|
|
|
}
|
2024-11-27 15:23:28 +08:00
|
|
|
|
},
|
2022-05-06 11:01:12 +08:00
|
|
|
|
{
|
2024-11-26 16:02:50 +08:00
|
|
|
|
path: "level2",
|
2024-11-27 15:23:28 +08:00
|
|
|
|
component: () => import("@/pages/demo/level2/index.vue"),
|
|
|
|
|
redirect: "/demo/level2/level3",
|
2024-11-26 16:02:50 +08:00
|
|
|
|
name: "Level2",
|
2022-09-30 17:41:47 +08:00
|
|
|
|
meta: {
|
2024-11-26 16:02:50 +08:00
|
|
|
|
title: "二级路由",
|
|
|
|
|
alwaysShow: true
|
2022-09-30 17:41:47 +08:00
|
|
|
|
},
|
2022-05-06 11:01:12 +08:00
|
|
|
|
children: [
|
|
|
|
|
{
|
2024-11-26 16:02:50 +08:00
|
|
|
|
path: "level3",
|
2024-11-27 15:23:28 +08:00
|
|
|
|
component: () => import("@/pages/demo/level2/level3/index.vue"),
|
2024-11-26 16:02:50 +08:00
|
|
|
|
name: "Level3",
|
2022-09-30 17:41:47 +08:00
|
|
|
|
meta: {
|
2024-11-26 16:02:50 +08:00
|
|
|
|
title: "三级路由",
|
2023-08-07 15:14:15 +08:00
|
|
|
|
keepAlive: true
|
2022-10-08 15:19:36 +08:00
|
|
|
|
}
|
2022-05-06 11:01:12 +08:00
|
|
|
|
}
|
|
|
|
|
]
|
2024-11-27 15:23:28 +08:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: "composable-demo",
|
|
|
|
|
redirect: "/demo/composable-demo/use-fetch-select",
|
|
|
|
|
name: "ComposableDemo",
|
|
|
|
|
meta: {
|
|
|
|
|
title: "组合式函数"
|
|
|
|
|
},
|
|
|
|
|
children: [
|
|
|
|
|
{
|
|
|
|
|
path: "use-fetch-select",
|
|
|
|
|
component: () => import("@/pages/demo/composable-demo/use-fetch-select.vue"),
|
|
|
|
|
name: "UseFetchSelect",
|
|
|
|
|
meta: {
|
|
|
|
|
title: "useFetchSelect"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: "use-fullscreen-loading",
|
|
|
|
|
component: () => import("@/pages/demo/composable-demo/use-fullscreen-loading.vue"),
|
|
|
|
|
name: "UseFullscreenLoading",
|
|
|
|
|
meta: {
|
|
|
|
|
title: "useFullscreenLoading"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
path: "use-watermark",
|
|
|
|
|
component: () => import("@/pages/demo/composable-demo/use-watermark.vue"),
|
|
|
|
|
name: "UseWatermark",
|
|
|
|
|
meta: {
|
|
|
|
|
title: "useWatermark"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
2022-05-06 11:01:12 +08:00
|
|
|
|
}
|
|
|
|
|
]
|
2023-01-05 17:15:18 +08:00
|
|
|
|
},
|
|
|
|
|
{
|
2024-11-27 15:23:28 +08:00
|
|
|
|
path: "/link",
|
2023-01-09 16:39:38 +08:00
|
|
|
|
meta: {
|
2024-11-27 19:13:47 +08:00
|
|
|
|
title: "文档链接",
|
2024-11-29 22:58:50 +08:00
|
|
|
|
elIcon: "Link"
|
2023-01-09 16:39:38 +08:00
|
|
|
|
},
|
2023-01-05 17:15:18 +08:00
|
|
|
|
children: [
|
|
|
|
|
{
|
2024-11-27 15:23:28 +08:00
|
|
|
|
path: "https://juejin.cn/post/7089377403717287972",
|
|
|
|
|
component: () => {},
|
|
|
|
|
name: "Link1",
|
2023-01-05 17:15:18 +08:00
|
|
|
|
meta: {
|
2024-11-27 15:23:28 +08:00
|
|
|
|
title: "中文文档"
|
2023-01-05 17:15:18 +08:00
|
|
|
|
}
|
2023-08-30 18:26:49 +08:00
|
|
|
|
},
|
|
|
|
|
{
|
2024-11-27 15:23:28 +08:00
|
|
|
|
path: "https://juejin.cn/column/7207659644487139387",
|
|
|
|
|
component: () => {},
|
|
|
|
|
name: "Link2",
|
2023-08-30 18:26:49 +08:00
|
|
|
|
meta: {
|
2024-11-27 15:23:28 +08:00
|
|
|
|
title: "新手教程"
|
2023-08-30 18:26:49 +08:00
|
|
|
|
}
|
2023-01-05 17:15:18 +08:00
|
|
|
|
}
|
|
|
|
|
]
|
2022-04-21 18:20:39 +08:00
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
/**
|
2024-11-25 14:21:18 +08:00
|
|
|
|
* @name 动态路由
|
|
|
|
|
* @description 用来放置有权限 (Roles 属性) 的路由
|
2024-11-27 15:23:28 +08:00
|
|
|
|
* @description 必须带有唯一的 Name 属性
|
2022-04-21 18:20:39 +08:00
|
|
|
|
*/
|
2024-02-25 19:26:35 +08:00
|
|
|
|
export const dynamicRoutes: RouteRecordRaw[] = [
|
2022-04-21 18:20:39 +08:00
|
|
|
|
{
|
2022-04-22 01:16:02 +08:00
|
|
|
|
path: "/permission",
|
2023-07-06 13:14:44 +08:00
|
|
|
|
component: Layouts,
|
2024-11-28 14:50:50 +08:00
|
|
|
|
redirect: "/permission/page-level",
|
2022-04-22 01:16:02 +08:00
|
|
|
|
name: "Permission",
|
2022-04-21 18:20:39 +08:00
|
|
|
|
meta: {
|
2024-11-27 19:13:47 +08:00
|
|
|
|
title: "权限演示",
|
2024-11-29 22:58:50 +08:00
|
|
|
|
elIcon: "Lock",
|
2024-11-28 13:39:44 +08:00
|
|
|
|
// 可以在根路由中设置角色
|
|
|
|
|
roles: ["admin", "editor"],
|
2024-11-19 20:14:23 +08:00
|
|
|
|
alwaysShow: true
|
2022-04-21 18:20:39 +08:00
|
|
|
|
},
|
|
|
|
|
children: [
|
|
|
|
|
{
|
2024-11-28 14:50:50 +08:00
|
|
|
|
path: "page-level",
|
|
|
|
|
component: () => import("@/pages/demo/permission/page-level.vue"),
|
|
|
|
|
name: "PermissionPageLevel",
|
2022-04-21 18:20:39 +08:00
|
|
|
|
meta: {
|
2024-03-08 21:12:48 +08:00
|
|
|
|
title: "页面级",
|
2024-11-28 13:39:44 +08:00
|
|
|
|
// 或者在子路由中设置角色
|
|
|
|
|
roles: ["admin"]
|
2022-04-21 18:20:39 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
2024-11-28 14:50:50 +08:00
|
|
|
|
path: "button-level",
|
|
|
|
|
component: () => import("@/pages/demo/permission/button-level.vue"),
|
|
|
|
|
name: "PermissionButtonLevel",
|
2022-04-21 18:20:39 +08:00
|
|
|
|
meta: {
|
2024-11-28 13:39:44 +08:00
|
|
|
|
title: "按钮级",
|
|
|
|
|
// 如果未设置角色,则表示:该页面不需要权限,但会继承根路由的角色
|
|
|
|
|
roles: undefined
|
2022-04-21 18:20:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
|
2024-11-26 14:45:42 +08:00
|
|
|
|
/** 路由实例 */
|
2024-11-14 17:33:12 +08:00
|
|
|
|
export const router = createRouter({
|
2024-11-26 14:45:42 +08:00
|
|
|
|
history: routerConfig.history,
|
|
|
|
|
routes: routerConfig.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
|
2022-04-21 18:20:39 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
/** 重置路由 */
|
|
|
|
|
export function resetRouter() {
|
|
|
|
|
try {
|
2024-11-28 13:39:44 +08:00
|
|
|
|
// 注意:所有动态路由路由必须带有 Name 属性,否则可能会不能完全重置干净
|
2022-04-21 18:20:39 +08:00
|
|
|
|
router.getRoutes().forEach((route) => {
|
|
|
|
|
const { name, meta } = route
|
|
|
|
|
if (name && meta.roles?.length) {
|
|
|
|
|
router.hasRoute(name) && router.removeRoute(name)
|
|
|
|
|
}
|
|
|
|
|
})
|
2024-11-19 10:42:02 +08:00
|
|
|
|
} catch {
|
2022-08-23 18:09:13 +08:00
|
|
|
|
// 强制刷新浏览器也行,只是交互体验不是很好
|
2024-11-28 13:39:44 +08:00
|
|
|
|
location.reload()
|
2022-04-21 18:20:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-26 14:45:42 +08:00
|
|
|
|
|
|
|
|
|
// 注册路由导航守卫
|
|
|
|
|
registerNavigationGuard(router)
|