2022-08-19 22:05:26 +08:00
|
|
|
import { createRouter, createWebHashHistory, createWebHistory, RouteRecordRaw } from "vue-router"
|
2022-04-22 01:16:02 +08:00
|
|
|
const Layout = () => import("@/layout/index.vue")
|
2022-04-21 18:20:39 +08:00
|
|
|
|
|
|
|
/** 常驻路由 */
|
|
|
|
export const constantRoutes: Array<RouteRecordRaw> = [
|
|
|
|
{
|
2022-04-22 01:16:02 +08:00
|
|
|
path: "/redirect",
|
2022-04-21 18:20:39 +08:00
|
|
|
component: Layout,
|
|
|
|
meta: {
|
|
|
|
hidden: true
|
|
|
|
},
|
|
|
|
children: [
|
|
|
|
{
|
2022-04-22 01:16:02 +08:00
|
|
|
path: "/redirect/:path(.*)",
|
|
|
|
component: () => import("@/views/redirect/index.vue")
|
2022-04-21 18:20:39 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2022-04-22 01:16:02 +08:00
|
|
|
path: "/login",
|
|
|
|
component: () => import("@/views/login/index.vue"),
|
2022-04-21 18:20:39 +08:00
|
|
|
meta: {
|
|
|
|
hidden: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2022-04-22 01:16:02 +08:00
|
|
|
path: "/",
|
2022-04-21 18:20:39 +08:00
|
|
|
component: Layout,
|
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",
|
|
|
|
component: () => import("@/views/dashboard/index.vue"),
|
|
|
|
name: "Dashboard",
|
2022-04-21 18:20:39 +08:00
|
|
|
meta: {
|
2022-04-22 01:16:02 +08:00
|
|
|
title: "首页",
|
|
|
|
icon: "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
|
|
|
{
|
|
|
|
path: "/unocss",
|
|
|
|
component: Layout,
|
|
|
|
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",
|
|
|
|
icon: "unocss"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2022-04-28 15:48:19 +08:00
|
|
|
{
|
|
|
|
path: "/link",
|
|
|
|
component: Layout,
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
path: "https://juejin.cn/post/7089377403717287972",
|
|
|
|
component: () => {},
|
|
|
|
name: "Link",
|
|
|
|
meta: {
|
|
|
|
title: "外链",
|
|
|
|
icon: "link"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2022-05-06 11:01:12 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/menu",
|
|
|
|
component: Layout,
|
|
|
|
redirect: "/menu/menu1",
|
|
|
|
name: "Menu",
|
|
|
|
meta: {
|
|
|
|
title: "多级菜单",
|
|
|
|
icon: "menu"
|
|
|
|
},
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
path: "menu1",
|
|
|
|
component: () => import("@/views/menu/menu1/index.vue"),
|
|
|
|
redirect: "/menu/menu1/menu1-1",
|
|
|
|
name: "Menu1",
|
|
|
|
meta: { title: "menu1" },
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
path: "menu1-1",
|
|
|
|
component: () => import("@/views/menu/menu1/menu1-1/index.vue"),
|
|
|
|
name: "Menu1-1",
|
|
|
|
meta: { title: "menu1-1" }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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" },
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
path: "menu1-2-1",
|
|
|
|
component: () => import("@/views/menu/menu1/menu1-2/menu1-2-1/index.vue"),
|
|
|
|
name: "Menu1-2-1",
|
|
|
|
meta: { title: "menu1-2-1" }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "menu1-2-2",
|
|
|
|
component: () => import("@/views/menu/menu1/menu1-2/menu1-2-2/index.vue"),
|
|
|
|
name: "Menu1-2-2",
|
|
|
|
meta: { title: "menu1-2-2" }
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "menu1-3",
|
|
|
|
component: () => import("@/views/menu/menu1/menu1-3/index.vue"),
|
|
|
|
name: "Menu1-3",
|
|
|
|
meta: { title: "menu1-3" }
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "menu2",
|
|
|
|
component: () => import("@/views/menu/menu2/index.vue"),
|
|
|
|
name: "Menu2",
|
|
|
|
meta: { title: "menu2" }
|
|
|
|
}
|
|
|
|
]
|
2022-04-21 18:20:39 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 动态路由
|
2022-07-01 16:25:51 +08:00
|
|
|
* 用来放置有权限 (Roles 属性) 的路由
|
|
|
|
* 必须带有 Name 属性
|
2022-04-21 18:20:39 +08:00
|
|
|
*/
|
|
|
|
export const asyncRoutes: Array<RouteRecordRaw> = [
|
|
|
|
{
|
2022-04-22 01:16:02 +08:00
|
|
|
path: "/permission",
|
2022-04-21 18:20:39 +08:00
|
|
|
component: Layout,
|
2022-04-22 01:16:02 +08:00
|
|
|
redirect: "/permission/page",
|
|
|
|
name: "Permission",
|
2022-04-21 18:20:39 +08:00
|
|
|
meta: {
|
2022-04-22 01:16:02 +08:00
|
|
|
title: "权限管理",
|
|
|
|
icon: "lock",
|
|
|
|
roles: ["admin", "editor"], // 可以在根路由中设置角色
|
2022-04-21 18:20:39 +08:00
|
|
|
alwaysShow: true // 将始终显示根菜单
|
|
|
|
},
|
|
|
|
children: [
|
|
|
|
{
|
2022-04-22 01:16:02 +08:00
|
|
|
path: "page",
|
|
|
|
component: () => import("@/views/permission/page.vue"),
|
|
|
|
name: "PagePermission",
|
2022-04-21 18:20:39 +08:00
|
|
|
meta: {
|
2022-04-22 01:16:02 +08:00
|
|
|
title: "页面权限",
|
|
|
|
roles: ["admin"] // 或者在子导航中设置角色
|
2022-04-21 18:20:39 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2022-04-22 01:16:02 +08:00
|
|
|
path: "directive",
|
|
|
|
component: () => import("@/views/permission/directive.vue"),
|
|
|
|
name: "DirectivePermission",
|
2022-04-21 18:20:39 +08:00
|
|
|
meta: {
|
2022-04-22 01:16:02 +08:00
|
|
|
title: "指令权限" // 如果未设置角色,则表示:该页面不需要权限,但会继承根路由的角色
|
2022-04-21 18:20:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2022-04-22 01:16:02 +08:00
|
|
|
path: "/:pathMatch(.*)*", // 必须将 'ErrorPage' 路由放在最后, Must put the 'ErrorPage' route at the end
|
2022-04-21 18:20:39 +08:00
|
|
|
component: Layout,
|
2022-04-22 01:16:02 +08:00
|
|
|
redirect: "/404",
|
|
|
|
name: "ErrorPage",
|
2022-04-21 18:20:39 +08:00
|
|
|
meta: {
|
2022-04-22 01:16:02 +08:00
|
|
|
title: "错误页面",
|
|
|
|
icon: "404",
|
2022-04-21 18:20:39 +08:00
|
|
|
hidden: true
|
|
|
|
},
|
|
|
|
children: [
|
|
|
|
{
|
2022-08-15 16:42:25 +08:00
|
|
|
path: "403",
|
|
|
|
component: () => import("@/views/error-page/403.vue"),
|
|
|
|
name: "403",
|
2022-04-21 18:20:39 +08:00
|
|
|
meta: {
|
2022-08-15 16:42:25 +08:00
|
|
|
title: "403"
|
2022-04-21 18:20:39 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2022-04-22 01:16:02 +08:00
|
|
|
path: "404",
|
|
|
|
component: () => import("@/views/error-page/404.vue"),
|
|
|
|
name: "404",
|
2022-04-21 18:20:39 +08:00
|
|
|
meta: {
|
2022-04-22 01:16:02 +08:00
|
|
|
title: "404"
|
2022-04-21 18:20:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const router = createRouter({
|
2022-08-19 22:05:26 +08:00
|
|
|
history:
|
|
|
|
import.meta.env.VITE_ROUTER_HISTORY === "hash"
|
|
|
|
? createWebHashHistory(import.meta.env.VITE_PUBLIC_PATH)
|
|
|
|
: createWebHistory(import.meta.env.VITE_PUBLIC_PATH),
|
2022-04-21 18:20:39 +08:00
|
|
|
routes: constantRoutes
|
|
|
|
})
|
|
|
|
|
|
|
|
/** 重置路由 */
|
|
|
|
export function resetRouter() {
|
2022-07-01 16:25:51 +08:00
|
|
|
// 注意:所有动态路由路由必须带有 Name 属性,否则可能会不能完全重置干净
|
2022-04-21 18:20:39 +08:00
|
|
|
try {
|
|
|
|
router.getRoutes().forEach((route) => {
|
|
|
|
const { name, meta } = route
|
|
|
|
if (name && meta.roles?.length) {
|
|
|
|
router.hasRoute(name) && router.removeRoute(name)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
// 强制刷新浏览器,不过体验不是很好
|
|
|
|
window.location.reload()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default router
|