2022-04-28 11:14:13 +08:00
|
|
|
|
import { useRoute, createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
|
2022-08-02 18:03:12 +08:00
|
|
|
|
import uniq from 'lodash/uniq';
|
2021-08-26 11:25:15 +08:00
|
|
|
|
|
2022-07-12 14:14:44 +08:00
|
|
|
|
// 自动导入modules文件夹下所有ts文件
|
|
|
|
|
const modules = import.meta.globEager('./modules/**/*.ts');
|
|
|
|
|
|
|
|
|
|
// 路由暂存
|
|
|
|
|
const routeModuleList: Array<RouteRecordRaw> = [];
|
|
|
|
|
|
|
|
|
|
Object.keys(modules).forEach((key) => {
|
|
|
|
|
const mod = modules[key].default || {};
|
|
|
|
|
const modList = Array.isArray(mod) ? [...mod] : [mod];
|
|
|
|
|
routeModuleList.push(...modList);
|
|
|
|
|
});
|
2021-08-26 11:25:15 +08:00
|
|
|
|
|
2022-02-28 18:28:14 +08:00
|
|
|
|
// 关于单层路由,meta 中设置 { single: true } 即可为单层路由,{ hidden: true } 即可在侧边栏隐藏该路由
|
|
|
|
|
|
2021-12-12 15:46:02 +08:00
|
|
|
|
// 存放动态路由
|
2022-07-12 14:14:44 +08:00
|
|
|
|
export const asyncRouterList: Array<RouteRecordRaw> = [...routeModuleList];
|
2021-08-26 11:25:15 +08:00
|
|
|
|
|
2021-12-12 15:46:02 +08:00
|
|
|
|
// 存放固定的路由
|
|
|
|
|
const defaultRouterList: Array<RouteRecordRaw> = [
|
|
|
|
|
{
|
|
|
|
|
path: '/login',
|
|
|
|
|
name: 'login',
|
|
|
|
|
component: () => import('@/pages/login/index.vue'),
|
|
|
|
|
},
|
2021-08-26 11:25:15 +08:00
|
|
|
|
{
|
2021-12-12 15:46:02 +08:00
|
|
|
|
path: '/',
|
2021-12-13 20:12:32 +08:00
|
|
|
|
redirect: '/dashboard/base',
|
2021-08-26 11:25:15 +08:00
|
|
|
|
},
|
2022-03-07 00:08:57 +08:00
|
|
|
|
{
|
|
|
|
|
path: '/:w+',
|
|
|
|
|
name: '404Page',
|
|
|
|
|
redirect: '/result/404',
|
|
|
|
|
},
|
2021-08-26 11:25:15 +08:00
|
|
|
|
];
|
|
|
|
|
|
2022-03-07 00:08:57 +08:00
|
|
|
|
export const allRoutes = [...defaultRouterList, ...asyncRouterList];
|
2021-12-12 15:46:02 +08:00
|
|
|
|
|
2022-08-02 18:03:12 +08:00
|
|
|
|
export const getRoutesExpanded = () => {
|
|
|
|
|
const expandedRoutes = [];
|
|
|
|
|
|
|
|
|
|
allRoutes.forEach((item) => {
|
|
|
|
|
if (item.meta && item.meta.expanded) {
|
|
|
|
|
expandedRoutes.push(item.path);
|
|
|
|
|
}
|
|
|
|
|
if (item.children && item.children.length > 0) {
|
|
|
|
|
item.children
|
|
|
|
|
.filter((child) => child.meta && child.meta.expanded)
|
|
|
|
|
.forEach((child: RouteRecordRaw) => {
|
|
|
|
|
expandedRoutes.push(item.path);
|
|
|
|
|
expandedRoutes.push(`${item.path}/${child.path}`);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return uniq(expandedRoutes);
|
|
|
|
|
};
|
|
|
|
|
|
2022-06-08 17:05:39 +08:00
|
|
|
|
export const getActive = (maxLevel = 3): string => {
|
2022-04-28 11:14:13 +08:00
|
|
|
|
const route = useRoute();
|
|
|
|
|
if (!route.path) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
return route.path
|
|
|
|
|
.split('/')
|
|
|
|
|
.filter((_item: string, index: number) => index <= maxLevel && index > 0)
|
|
|
|
|
.map((item: string) => `/${item}`)
|
|
|
|
|
.join('');
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-26 11:25:15 +08:00
|
|
|
|
const router = createRouter({
|
2021-12-23 10:45:56 +08:00
|
|
|
|
history: createWebHashHistory(),
|
2022-03-07 00:08:57 +08:00
|
|
|
|
routes: allRoutes,
|
2021-09-01 11:26:19 +08:00
|
|
|
|
scrollBehavior() {
|
2021-08-26 11:25:15 +08:00
|
|
|
|
return {
|
|
|
|
|
el: '#app',
|
|
|
|
|
top: 0,
|
|
|
|
|
behavior: 'smooth',
|
2021-09-01 11:26:19 +08:00
|
|
|
|
};
|
2021-08-26 11:25:15 +08:00
|
|
|
|
},
|
2021-09-01 11:26:19 +08:00
|
|
|
|
});
|
2021-12-12 15:46:02 +08:00
|
|
|
|
|
2021-09-01 11:26:19 +08:00
|
|
|
|
export default router;
|