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

314 lines
7.3 KiB
TypeScript
Raw Normal View History

import { type RouteRecordRaw, createRouter } from "vue-router"
import { history, flatMultiLevelRoutes } from "./helper"
import routeSettings from "@/config/route"
2022-10-18 15:07:42 +08:00
export const Layouts = () => import("@/layouts/index.vue")
/**
*
* 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("@/views/redirect/index.vue")
}
]
},
{
path: "/403",
component: () => import("@/views/error-page/403.vue"),
meta: {
hidden: true
}
},
{
path: "/404",
component: () => import("@/views/error-page/404.vue"),
meta: {
hidden: true
},
alias: "/:pathMatch(.*)*"
},
{
path: "/login",
component: () => import("@/views/login/index.vue"),
meta: {
hidden: true
}
},
{
path: "/",
2023-07-06 13:14:44 +08:00
component: Layouts,
redirect: "/dashboard",
children: [
{
path: "dashboard",
component: () => import("@/views/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: "/unocss",
2023-07-06 13:14:44 +08:00
component: Layouts,
2022-05-12 19:07:54 +08:00
redirect: "/unocss/index",
children: [
{
path: "index",
component: () => import("@/views/unocss/index.vue"),
2022-08-15 16:34:05 +08:00
name: "UnoCSS",
2022-05-12 19:07:54 +08:00
meta: {
title: "UnoCSS",
svgIcon: "unocss"
2022-05-12 19:07:54 +08:00
}
}
]
},
2022-04-28 15:48:19 +08:00
{
path: "/link",
meta: {
title: "外链",
svgIcon: "link"
},
2022-04-28 15:48:19 +08:00
children: [
{
path: "https://juejin.cn/post/7089377403717287972",
component: () => {},
name: "Link1",
meta: {
title: "中文文档"
}
},
{
path: "https://juejin.cn/column/7207659644487139387",
component: () => {},
name: "Link2",
2022-04-28 15:48:19 +08:00
meta: {
title: "新手教程"
2022-04-28 15:48:19 +08:00
}
}
]
2022-05-06 11:01:12 +08:00
},
2022-10-20 20:58:57 +08:00
{
path: "/table",
2023-07-06 13:14:44 +08:00
component: Layouts,
2022-10-20 20:58:57 +08:00
redirect: "/table/element-plus",
name: "Table",
meta: {
title: "表格",
elIcon: "Grid"
},
children: [
{
path: "element-plus",
component: () => import("@/views/table/element-plus/index.vue"),
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("@/views/table/vxe-table/index.vue"),
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: "/menu",
2023-07-06 13:14:44 +08:00
component: Layouts,
2022-05-06 11:01:12 +08:00
redirect: "/menu/menu1",
name: "Menu",
meta: {
title: "多级路由",
svgIcon: "menu"
2022-05-06 11:01:12 +08:00
},
children: [
{
path: "menu1",
component: () => import("@/views/menu/menu1/index.vue"),
redirect: "/menu/menu1/menu1-1",
name: "Menu1",
meta: {
title: "menu1"
},
2022-05-06 11:01:12 +08:00
children: [
{
path: "menu1-1",
component: () => import("@/views/menu/menu1/menu1-1/index.vue"),
name: "Menu1-1",
meta: {
title: "menu1-1",
keepAlive: true
}
2022-05-06 11:01:12 +08:00
},
{
path: "menu1-2",
component: () => import("@/views/menu/menu1/menu1-2/index.vue"),
redirect: "/menu/menu1/menu1-2/menu1-2-1",
name: "Menu1-2",
meta: {
title: "menu1-2"
},
2022-05-06 11:01:12 +08:00
children: [
{
path: "menu1-2-1",
component: () => import("@/views/menu/menu1/menu1-2/menu1-2-1/index.vue"),
name: "Menu1-2-1",
2022-10-08 15:19:36 +08:00
meta: {
title: "menu1-2-1",
keepAlive: true
2022-10-08 15:19:36 +08:00
}
2022-05-06 11:01:12 +08:00
},
{
path: "menu1-2-2",
component: () => import("@/views/menu/menu1/menu1-2/menu1-2-2/index.vue"),
name: "Menu1-2-2",
2022-10-08 15:19:36 +08:00
meta: {
title: "menu1-2-2",
keepAlive: true
2022-10-08 15:19:36 +08:00
}
2022-05-06 11:01:12 +08:00
}
]
},
{
path: "menu1-3",
component: () => import("@/views/menu/menu1/menu1-3/index.vue"),
name: "Menu1-3",
2022-10-08 15:19:36 +08:00
meta: {
title: "menu1-3",
keepAlive: true
2022-10-08 15:19:36 +08:00
}
2022-05-06 11:01:12 +08:00
}
]
},
{
path: "menu2",
component: () => import("@/views/menu/menu2/index.vue"),
name: "Menu2",
2022-10-08 15:19:36 +08:00
meta: {
title: "menu2",
keepAlive: true
2022-10-08 15:19:36 +08:00
}
2022-05-06 11:01:12 +08:00
}
]
2023-01-05 17:15:18 +08:00
},
{
path: "/hook-demo",
2023-07-06 13:14:44 +08:00
component: Layouts,
redirect: "/hook-demo/use-fetch-select",
name: "HookDemo",
2023-01-09 16:39:38 +08:00
meta: {
2024-03-08 21:12:48 +08:00
title: "Hook",
2023-01-09 16:39:38 +08:00
elIcon: "Menu",
alwaysShow: true
},
2023-01-05 17:15:18 +08:00
children: [
{
2023-01-09 16:39:38 +08:00
path: "use-fetch-select",
component: () => import("@/views/hook-demo/use-fetch-select.vue"),
2023-01-09 16:39:38 +08:00
name: "UseFetchSelect",
meta: {
title: "useFetchSelect"
}
},
{
path: "use-fullscreen-loading",
component: () => import("@/views/hook-demo/use-fullscreen-loading.vue"),
2023-01-09 16:39:38 +08:00
name: "UseFullscreenLoading",
2023-01-05 17:15:18 +08:00
meta: {
2023-01-09 16:39:38 +08:00
title: "useFullscreenLoading"
2023-01-05 17:15:18 +08:00
}
2023-08-30 18:26:49 +08:00
},
{
path: "use-watermark",
component: () => import("@/views/hook-demo/use-watermark.vue"),
name: "UseWatermark",
meta: {
title: "useWatermark"
}
2023-01-05 17:15:18 +08:00
}
]
}
]
/**
*
* (Roles )
* Name
*/
export const dynamicRoutes: RouteRecordRaw[] = [
{
path: "/permission",
2023-07-06 13:14:44 +08:00
component: Layouts,
redirect: "/permission/page",
name: "Permission",
meta: {
2024-03-08 21:12:48 +08:00
title: "权限",
svgIcon: "lock",
roles: ["admin", "editor"], // 可以在根路由中设置角色
alwaysShow: true // 将始终显示根菜单
},
children: [
{
path: "page",
component: () => import("@/views/permission/page.vue"),
name: "PagePermission",
meta: {
2024-03-08 21:12:48 +08:00
title: "页面级",
roles: ["admin"] // 或者在子导航中设置角色
}
},
{
path: "directive",
component: () => import("@/views/permission/directive.vue"),
name: "DirectivePermission",
meta: {
2024-03-08 21:12:48 +08:00
title: "按钮级" // 如果未设置角色,则表示:该页面不需要权限,但会继承根路由的角色
}
}
]
}
]
const router = createRouter({
history,
routes: routeSettings.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})
/** 重置路由 */
export function resetRouter() {
// 注意:所有动态路由路由必须带有 Name 属性,否则可能会不能完全重置干净
try {
router.getRoutes().forEach((route) => {
const { name, meta } = route
if (name && meta.roles?.length) {
router.hasRoute(name) && router.removeRoute(name)
}
})
2023-06-01 09:26:53 +08:00
} catch {
// 强制刷新浏览器也行,只是交互体验不是很好
window.location.reload()
}
}
export default router