Skip to content

Commit 6bc30aa

Browse files
committed
feat(router): add getRoutes
1 parent 3cfa151 commit 6bc30aa

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

flow/declarations.js

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ declare type RouteConfig = {
6565

6666
declare type RouteRecord = {
6767
path: string;
68+
alias: Array<string>;
6869
regex: RouteRegExp;
6970
components: Dictionary<any>;
7071
instances: Dictionary<any>;

src/create-matcher.js

+18
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type Matcher = {
1313
match: (raw: RawLocation, current?: Route, redirectedFrom?: Location) => Route;
1414
addRoutes: (routes: Array<RouteConfig>) => void;
1515
addRoute: (parentNameOrRoute: string | RouteConfig, route?: RouteConfig) => void;
16+
getRoutes: () => Array<RouteRecord>;
1617
};
1718

1819
export function createMatcher (
@@ -29,6 +30,22 @@ export function createMatcher (
2930
const parent = (typeof parentOrRoute !== 'object') ? nameMap[parentOrRoute] : undefined
3031
// $flow-disable-line
3132
createRouteMap([route || parentOrRoute], pathList, pathMap, nameMap, parent)
33+
34+
// add aliases of parent
35+
if (parent) {
36+
createRouteMap(
37+
// $flow-disable-line route is defined if parent is
38+
parent.alias.map(alias => ({ path: alias, children: [route] })),
39+
pathList,
40+
pathMap,
41+
nameMap,
42+
parent
43+
)
44+
}
45+
}
46+
47+
function getRoutes () {
48+
return pathList.map(path => pathMap[path])
3249
}
3350

3451
function match (
@@ -175,6 +192,7 @@ export function createMatcher (
175192
return {
176193
match,
177194
addRoute,
195+
getRoutes,
178196
addRoutes
179197
}
180198
}

src/create-route-map.js

+5
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ function addRouteRecord (
9393
path: normalizedPath,
9494
regex: compileRouteRegex(normalizedPath, pathToRegexpOptions),
9595
components: route.components || { default: route.component },
96+
alias: route.alias
97+
? typeof route.alias === 'string'
98+
? [route.alias]
99+
: route.alias
100+
: [],
96101
instances: {},
97102
enteredCbs: {},
98103
name,

src/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ export default class VueRouter {
244244
}
245245
}
246246

247+
getRoutes () {
248+
return this.matcher.getRoutes()
249+
}
250+
247251
addRoute (parentOrRoute: string | RouteConfig, route?: RouteConfig) {
248252
this.matcher.addRoute(parentOrRoute, route)
249253
if (this.history.current !== START) {

test/unit/specs/create-matcher.spec.js

+19
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@ describe('Creating Matcher', function () {
5454
expect(matcher.match('/p/c/n').name).toBe('nested')
5555
})
5656

57+
it('can get all routes', function () {
58+
const component = { name: 'fake' }
59+
const matcher = createMatcher([])
60+
61+
expect(matcher.getRoutes()).toEqual([])
62+
63+
matcher.addRoute({ path: '/b', name: 'b', component })
64+
expect(matcher.getRoutes().length).toBe(1)
65+
66+
matcher.addRoute({ path: '/c', name: 'c', alias: ['/a', '/d'], component })
67+
expect(matcher.getRoutes().length).toBe(4)
68+
69+
matcher.addRoute('b', { path: 'd', component })
70+
expect(matcher.getRoutes().length).toBe(5)
71+
72+
matcher.addRoute('c', { path: 'd', component })
73+
expect(matcher.getRoutes().length).toBe(8)
74+
})
75+
5776
it('in development, has logged a warning if a named route does not exist', function () {
5877
process.env.NODE_ENV = 'development'
5978
const { name, matched } = match({ name: 'bar' }, routes[0])

0 commit comments

Comments
 (0)