Skip to content

Commit

Permalink
feat(router): add getRoutes
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jan 4, 2021
1 parent 3cfa151 commit 6bc30aa
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions flow/declarations.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ declare type RouteConfig = {

declare type RouteRecord = {
path: string;
alias: Array<string>;
regex: RouteRegExp;
components: Dictionary<any>;
instances: Dictionary<any>;
Expand Down
18 changes: 18 additions & 0 deletions src/create-matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type Matcher = {
match: (raw: RawLocation, current?: Route, redirectedFrom?: Location) => Route;
addRoutes: (routes: Array<RouteConfig>) => void;
addRoute: (parentNameOrRoute: string | RouteConfig, route?: RouteConfig) => void;
getRoutes: () => Array<RouteRecord>;
};

export function createMatcher (
Expand All @@ -29,6 +30,22 @@ export function createMatcher (
const parent = (typeof parentOrRoute !== 'object') ? nameMap[parentOrRoute] : undefined
// $flow-disable-line
createRouteMap([route || parentOrRoute], pathList, pathMap, nameMap, parent)

// add aliases of parent
if (parent) {
createRouteMap(
// $flow-disable-line route is defined if parent is
parent.alias.map(alias => ({ path: alias, children: [route] })),
pathList,
pathMap,
nameMap,
parent
)
}
}

function getRoutes () {
return pathList.map(path => pathMap[path])
}

function match (
Expand Down Expand Up @@ -175,6 +192,7 @@ export function createMatcher (
return {
match,
addRoute,
getRoutes,
addRoutes
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/create-route-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ function addRouteRecord (
path: normalizedPath,
regex: compileRouteRegex(normalizedPath, pathToRegexpOptions),
components: route.components || { default: route.component },
alias: route.alias
? typeof route.alias === 'string'
? [route.alias]
: route.alias
: [],
instances: {},
enteredCbs: {},
name,
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ export default class VueRouter {
}
}

getRoutes () {
return this.matcher.getRoutes()
}

addRoute (parentOrRoute: string | RouteConfig, route?: RouteConfig) {
this.matcher.addRoute(parentOrRoute, route)
if (this.history.current !== START) {
Expand Down
19 changes: 19 additions & 0 deletions test/unit/specs/create-matcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ describe('Creating Matcher', function () {
expect(matcher.match('/p/c/n').name).toBe('nested')
})

it('can get all routes', function () {
const component = { name: 'fake' }
const matcher = createMatcher([])

expect(matcher.getRoutes()).toEqual([])

matcher.addRoute({ path: '/b', name: 'b', component })
expect(matcher.getRoutes().length).toBe(1)

matcher.addRoute({ path: '/c', name: 'c', alias: ['/a', '/d'], component })
expect(matcher.getRoutes().length).toBe(4)

matcher.addRoute('b', { path: 'd', component })
expect(matcher.getRoutes().length).toBe(5)

matcher.addRoute('c', { path: 'd', component })
expect(matcher.getRoutes().length).toBe(8)
})

it('in development, has logged a warning if a named route does not exist', function () {
process.env.NODE_ENV = 'development'
const { name, matched } = match({ name: 'bar' }, routes[0])
Expand Down

0 comments on commit 6bc30aa

Please sign in to comment.