-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
139 lines (127 loc) · 3.02 KB
/
router.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import pathToRegexp from "path-to-regexp";
import _ from "lodash";
/**
* 嵌套路由扁平化
* @param {*} routers
* @param {*} prefix
* @param {*} routeMap
*
* const routers = [
{
path: "/",
name: "home",
component: "Home",
children: [
{
path: "about",
name: "about",
component: () =>
import("../views/About.vue")
}
]
}
]; ====> this.translator(routes)
{ '/': { name: 'home', component: 'Home', path: '/' },
'/about':
{ name: 'about',
component: [Function: component],
path: '/about' } }
*/
export function translator(routers, prefix = "", routeMap = {}) {
routers.forEach(route => {
const { children, path, ...props } = route;
prefix += path;
routeMap[prefix] = { ...props, path: prefix };
if (children) {
this.translator(children, prefix, routeMap);
}
});
return routeMap;
}
/**
* Vue-router中嵌套路由的实现
* @param {*} routers
*/
export function createRouteMap(routers) {
routers.forEach(route => {
addRouteRecord(route);
});
}
const addRouteRecord = (pathMap = {}, route, parent) => {
const { path } = route;
const normalizedPath = normalizePath(path, parent);
if (route.children) {
route.children.forEach(child => {
addRouteRecord(pathMap, child, { ...route, path: normalizedPath });
});
}
if (!pathMap[normalizedPath]) {
pathMap[normalizedPath] = route;
}
};
const normalizePath = (path, parent) => {
if (path[0] === "/") return path;
if (parent == null) return path;
return `${parent.path}/${path}`.replace(/\/\//g, "/");
};
export function getPlainNode(nodeList, parentPath = "") {
const arr = [];
nodeList.forEach(node => {
const item = node;
item.path = `${parentPath}/${item.path || ""}`.replace(/\/+/g, "/");
item.exact = true;
if (item.children) {
arr.push(...getPlainNode(item.children, item.path));
} else {
if (!item.children) {
item.exact = false;
}
arr.push(item);
}
});
return arr;
}
function getRouterMap(item, map, pPath = "") {
if (_.isArray(item)) {
item.forEach(x => {
return getRouterMap(x, map, item.path);
});
} else {
let path = "";
if (
_.indexOf(item.path, "/") === 0 ||
_.lastIndexOf(pPath, "/") === pPath.length - 1
) {
path = `${pPath}${item.path}`;
} else {
path = `${pPath}/${item.path}`;
}
map.set(path, item);
if (_.isEmpty(item.children)) {
return;
}
item.children.forEach(x => {
return getRouterMap(x, map, path);
});
}
}
/**
* 通过path获取路径
* @param {路由path} path
*/
export function getRouterByPath(path, routerData) {
if (_.isEmpty(path)) {
return null;
}
// 将routerData转换成map数据接口
let routerMap = new Map();
getRouterMap(routerData || [], routerMap);
let item = null;
routerMap.forEach((value, key) => {
if (pathToRegexp(key).exec(path)) {
item = value;
return false;
}
});
return _.isEmpty(item) ? null : item;
}