Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 16430de

Browse files
committedApr 14, 2017
use explicit pathList for matching order
1 parent d763544 commit 16430de

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed
 

‎src/create-matcher.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ export function createMatcher (
1717
routes: Array<RouteConfig>,
1818
router: VueRouter
1919
): Matcher {
20-
const { pathMap, nameMap } = createRouteMap(routes)
20+
const { pathList, pathMap, nameMap } = createRouteMap(routes)
2121

2222
function addRoutes (routes) {
23-
createRouteMap(routes, pathMap, nameMap)
23+
createRouteMap(routes, pathList, pathMap, nameMap)
2424
}
2525

2626
function match (
@@ -58,7 +58,8 @@ export function createMatcher (
5858
}
5959
} else if (location.path) {
6060
location.params = {}
61-
for (const path in pathMap) {
61+
for (let i = 0; i < pathList.length; i++) {
62+
const path = pathList[i]
6263
if (matchRoute(path, location.params, location.path)) {
6364
return _createRoute(pathMap[path], location, redirectedFrom)
6465
}

‎src/create-route-map.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,32 @@ import { cleanPath } from './util/path'
55

66
export function createRouteMap (
77
routes: Array<RouteConfig>,
8+
oldPathList?: Array<string>,
89
oldPathMap?: Dictionary<RouteRecord>,
910
oldNameMap?: Dictionary<RouteRecord>
1011
): {
12+
pathList: Array<string>;
1113
pathMap: Dictionary<RouteRecord>;
1214
nameMap: Dictionary<RouteRecord>;
1315
} {
16+
// the path list is used to control path matching priority
17+
const pathList: Array<string> = oldPathList || []
1418
const pathMap: Dictionary<RouteRecord> = oldPathMap || Object.create(null)
1519
const nameMap: Dictionary<RouteRecord> = oldNameMap || Object.create(null)
1620

1721
routes.forEach(route => {
18-
addRouteRecord(pathMap, nameMap, route)
22+
addRouteRecord(pathList, pathMap, nameMap, route)
1923
})
2024

2125
return {
26+
pathList,
2227
pathMap,
2328
nameMap
2429
}
2530
}
2631

2732
function addRouteRecord (
33+
pathList: Array<string>,
2834
pathMap: Dictionary<RouteRecord>,
2935
nameMap: Dictionary<RouteRecord>,
3036
route: RouteConfig,
@@ -78,7 +84,7 @@ function addRouteRecord (
7884
const childMatchAs = matchAs
7985
? cleanPath(`${matchAs}/${child.path}`)
8086
: undefined
81-
addRouteRecord(pathMap, nameMap, child, record, childMatchAs)
87+
addRouteRecord(pathList, pathMap, nameMap, child, record, childMatchAs)
8288
})
8389
}
8490

@@ -89,18 +95,19 @@ function addRouteRecord (
8995
path: alias,
9096
children: route.children
9197
}
92-
addRouteRecord(pathMap, nameMap, aliasRoute, parent, record.path)
98+
addRouteRecord(pathList, pathMap, nameMap, aliasRoute, parent, record.path)
9399
})
94100
} else {
95101
const aliasRoute = {
96102
path: route.alias,
97103
children: route.children
98104
}
99-
addRouteRecord(pathMap, nameMap, aliasRoute, parent, record.path)
105+
addRouteRecord(pathList, pathMap, nameMap, aliasRoute, parent, record.path)
100106
}
101107
}
102108

103109
if (!pathMap[record.path]) {
110+
pathList.push(record.path)
104111
pathMap[record.path] = record
105112
}
106113

0 commit comments

Comments
 (0)
Please sign in to comment.