This repository was archived by the owner on Dec 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
84 lines (72 loc) · 2.09 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { useRoute, createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import uniq from 'lodash/uniq';
// 自动导入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);
});
// 关于单层路由,meta 中设置 { single: true } 即可为单层路由,{ hidden: true } 即可在侧边栏隐藏该路由
// 存放动态路由
export const asyncRouterList: Array<RouteRecordRaw> = [...routeModuleList];
// 存放固定的路由
const defaultRouterList: Array<RouteRecordRaw> = [
{
path: '/login',
name: 'login',
component: () => import('@/pages/login/index.vue'),
},
{
path: '/',
redirect: '/dashboard/base',
},
{
path: '/:w+',
name: '404Page',
redirect: '/result/404',
},
];
export const allRoutes = [...defaultRouterList, ...asyncRouterList];
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);
};
export const getActive = (maxLevel = 3): string => {
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('');
};
const router = createRouter({
history: createWebHashHistory(),
routes: allRoutes,
scrollBehavior() {
return {
el: '#app',
top: 0,
behavior: 'smooth',
};
},
});
export default router;