271 lines
6.2 KiB
TypeScript

import type { RouteRecordRaw } from "vue-router"
import { routerConfig } from "@/router/config"
import { registerNavigationGuard } from "@/router/guard"
import { createRouter } from "vue-router"
import { flatMultiLevelRoutes } from "./helper"
const Layouts = () => import("@/layouts/index.vue")
/**
* @name 常驻路由
* @description 除了 redirect/403/404/login 等隐藏页面,其他页面建议设置 Name 属性
*/
export const constantRoutes: RouteRecordRaw[] = [
{
path: "/redirect",
component: Layouts,
meta: {
hidden: true
},
children: [
{
path: ":path(.*)",
component: () => import("@/pages/redirect/index.vue")
}
]
},
{
path: "/403",
component: () => import("@/pages/error-page/403.vue"),
meta: {
hidden: true
}
},
{
path: "/404",
component: () => import("@/pages/error-page/404.vue"),
meta: {
hidden: true
},
alias: "/:pathMatch(.*)*"
},
{
path: "/login",
component: () => import("@/pages/login/index.vue"),
meta: {
hidden: true
}
},
{
path: "/",
component: Layouts,
redirect: "/dashboard",
children: [
{
path: "dashboard",
component: () => import("@/pages/dashboard/index.vue"),
name: "Dashboard",
meta: {
title: "首页",
svgIcon: "dashboard",
affix: true
}
}
]
},
{
path: "/unocss",
component: Layouts,
redirect: "/unocss/index",
children: [
{
path: "index",
component: () => import("@/pages/unocss/index.vue"),
name: "UnoCSS",
meta: {
title: "UnoCSS",
svgIcon: "unocss"
}
}
]
},
{
path: "/link",
meta: {
title: "外链",
svgIcon: "link"
},
children: [
{
path: "https://juejin.cn/post/7089377403717287972",
component: () => {},
name: "Link1",
meta: {
title: "中文文档"
}
},
{
path: "https://juejin.cn/column/7207659644487139387",
component: () => {},
name: "Link2",
meta: {
title: "新手教程"
}
}
]
},
{
path: "/table",
component: Layouts,
redirect: "/table/element-plus",
name: "Table",
meta: {
title: "表格",
elIcon: "Grid"
},
children: [
{
path: "element-plus",
component: () => import("@/pages/table/element-plus/index.vue"),
name: "ElementPlus",
meta: {
title: "Element Plus",
keepAlive: true
}
},
{
path: "vxe-table",
component: () => import("@/pages/table/vxe-table/index.vue"),
name: "VxeTable",
meta: {
title: "Vxe Table",
keepAlive: true
}
}
]
},
{
path: "/level1",
component: Layouts,
redirect: "/level1/level2",
name: "Level1",
meta: {
title: "一级路由",
svgIcon: "level"
},
children: [
{
path: "level2",
component: () => import("@/pages/level1/level2/index.vue"),
redirect: "/level1/level2/level3",
name: "Level2",
meta: {
title: "二级路由",
alwaysShow: true
},
children: [
{
path: "level3",
component: () => import("@/pages/level1/level2/level3/index.vue"),
name: "Level3",
meta: {
title: "三级路由",
keepAlive: true
}
}
]
}
]
},
{
path: "/composable-demo",
component: Layouts,
redirect: "/composable-demo/use-fetch-select",
name: "ComposableDemo",
meta: {
title: "组合式函数",
elIcon: "Menu"
},
children: [
{
path: "use-fetch-select",
component: () => import("@/pages/composable-demo/use-fetch-select.vue"),
name: "UseFetchSelect",
meta: {
title: "useFetchSelect"
}
},
{
path: "use-fullscreen-loading",
component: () => import("@/pages/composable-demo/use-fullscreen-loading.vue"),
name: "UseFullscreenLoading",
meta: {
title: "useFullscreenLoading"
}
},
{
path: "use-watermark",
component: () => import("@/pages/composable-demo/use-watermark.vue"),
name: "UseWatermark",
meta: {
title: "useWatermark"
}
}
]
}
]
/**
* @name 动态路由
* @description 用来放置有权限 (Roles 属性) 的路由
* @description 必须带有 Name 属性
*/
export const dynamicRoutes: RouteRecordRaw[] = [
{
path: "/permission",
component: Layouts,
redirect: "/permission/page",
name: "Permission",
meta: {
title: "权限",
svgIcon: "lock",
roles: ["admin", "editor"], // 可以在根路由中设置角色
alwaysShow: true
},
children: [
{
path: "page",
component: () => import("@/pages/permission/page.vue"),
name: "PagePermission",
meta: {
title: "页面级",
roles: ["admin"] // 或者在子导航中设置角色
}
},
{
path: "directive",
component: () => import("@/pages/permission/directive.vue"),
name: "DirectivePermission",
meta: {
title: "按钮级" // 如果未设置角色,则表示:该页面不需要权限,但会继承根路由的角色
}
}
]
}
]
/** 路由实例 */
export const router = createRouter({
history: routerConfig.history,
routes: routerConfig.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)
}
})
} catch {
// 强制刷新浏览器也行,只是交互体验不是很好
window.location.reload()
}
}
// 注册路由导航守卫
registerNavigationGuard(router)