Template
1
0
mirror of https://github.com/un-pany/v3-admin-vite.git synced 2025-04-23 04:19:20 +08:00

253 lines
6.1 KiB
TypeScript
Raw Normal View History

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")
/**
* @name
* @description redirect/403/404/login Name
*/
export const constantRoutes: RouteRecordRaw[] = [
{
path: "/redirect",
2023-07-06 13:14:44 +08:00
component: Layouts,
meta: {
hidden: true
},
children: [
{
path: ":path(.*)",
component: () => import("@/pages/redirect/index.vue")
}
]
},
{
path: "/403",
2024-11-27 14:12:44 +08:00
component: () => import("@/pages/error/403.vue"),
meta: {
hidden: true
}
},
{
path: "/404",
2024-11-27 14:12:44 +08:00
component: () => import("@/pages/error/404.vue"),
meta: {
hidden: true
},
alias: "/:pathMatch(.*)*"
},
{
path: "/login",
component: () => import("@/pages/login/index.vue"),
meta: {
hidden: true
}
},
{
path: "/",
2023-07-06 13:14:44 +08:00
component: Layouts,
redirect: "/dashboard",
children: [
{
path: "dashboard",
component: () => import("@/pages/dashboard/index.vue"),
name: "Dashboard",
meta: {
title: "首页",
svgIcon: "dashboard",
affix: true
}
}
]
2022-04-28 15:48:19 +08:00
},
2022-05-12 19:07:54 +08:00
{
path: "/demo",
2023-07-06 13:14:44 +08:00
component: Layouts,
redirect: "/demo/unocss",
name: "Demo",
meta: {
2024-11-27 19:13:47 +08:00
title: "示例集合",
elIcon: "DataBoard"
},
2022-04-28 15:48:19 +08:00
children: [
{
path: "unocss",
component: () => import("@/pages/demo/unocss/index.vue"),
name: "UnoCSS",
meta: {
title: "UnoCSS"
}
},
2022-10-20 20:58:57 +08:00
{
path: "element-plus",
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",
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
}
},
2022-05-06 11:01:12 +08:00
{
path: "level2",
component: () => import("@/pages/demo/level2/index.vue"),
redirect: "/demo/level2/level3",
name: "Level2",
meta: {
title: "二级路由",
alwaysShow: true
},
2022-05-06 11:01:12 +08:00
children: [
{
path: "level3",
component: () => import("@/pages/demo/level2/level3/index.vue"),
name: "Level3",
meta: {
title: "三级路由",
keepAlive: true
2022-10-08 15:19:36 +08:00
}
2022-05-06 11:01:12 +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
},
{
path: "/link",
2023-01-09 16:39:38 +08:00
meta: {
2024-11-27 19:13:47 +08:00
title: "文档链接",
elIcon: "Link"
2023-01-09 16:39:38 +08:00
},
2023-01-05 17:15:18 +08:00
children: [
{
path: "https://juejin.cn/post/7089377403717287972",
component: () => {},
name: "Link1",
2023-01-05 17:15:18 +08:00
meta: {
title: "中文文档"
2023-01-05 17:15:18 +08:00
}
2023-08-30 18:26:49 +08:00
},
{
path: "https://juejin.cn/column/7207659644487139387",
component: () => {},
name: "Link2",
2023-08-30 18:26:49 +08:00
meta: {
title: "新手教程"
2023-08-30 18:26:49 +08:00
}
2023-01-05 17:15:18 +08:00
}
]
}
]
/**
* @name
* @description (Roles )
* @description Name
*/
export const dynamicRoutes: RouteRecordRaw[] = [
{
path: "/permission",
2023-07-06 13:14:44 +08:00
component: Layouts,
2024-11-28 14:50:50 +08:00
redirect: "/permission/page-level",
name: "Permission",
meta: {
2024-11-27 19:13:47 +08:00
title: "权限演示",
elIcon: "Lock",
2024-11-28 13:39:44 +08:00
// 可以在根路由中设置角色
roles: ["admin", "editor"],
2024-11-19 20:14:23 +08:00
alwaysShow: true
},
children: [
{
2024-11-28 14:50:50 +08:00
path: "page-level",
component: () => import("@/pages/demo/permission/page-level.vue"),
name: "PermissionPageLevel",
meta: {
2024-03-08 21:12:48 +08:00
title: "页面级",
2024-11-28 13:39:44 +08:00
// 或者在子路由中设置角色
roles: ["admin"]
}
},
{
2024-11-28 14:50:50 +08:00
path: "button-level",
component: () => import("@/pages/demo/permission/button-level.vue"),
name: "PermissionButtonLevel",
meta: {
2024-11-28 13:39:44 +08:00
title: "按钮级",
// 如果未设置角色,则表示:该页面不需要权限,但会继承根路由的角色
roles: undefined
}
}
]
}
]
2024-11-26 14:45:42 +08:00
/** 路由实例 */
export const router = createRouter({
2024-11-26 14:45:42 +08:00
history: routerConfig.history,
routes: routerConfig.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})
/** 重置路由 */
export function resetRouter() {
try {
2024-11-28 13:39:44 +08:00
// 注意:所有动态路由路由必须带有 Name 属性,否则可能会不能完全重置干净
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 {
// 强制刷新浏览器也行,只是交互体验不是很好
2024-11-28 13:39:44 +08:00
location.reload()
}
}
2024-11-26 14:45:42 +08:00
// 注册路由导航守卫
registerNavigationGuard(router)