From 6bc30aa4b6b1bbcb851b6de8c8e55de596d19b34 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Mon, 4 Jan 2021 16:34:02 +0100 Subject: [PATCH] feat(router): add getRoutes --- flow/declarations.js | 1 + src/create-matcher.js | 18 ++++++++++++++++++ src/create-route-map.js | 5 +++++ src/index.js | 4 ++++ test/unit/specs/create-matcher.spec.js | 19 +++++++++++++++++++ 5 files changed, 47 insertions(+) diff --git a/flow/declarations.js b/flow/declarations.js index ee1a10283..824c3eca0 100644 --- a/flow/declarations.js +++ b/flow/declarations.js @@ -65,6 +65,7 @@ declare type RouteConfig = { declare type RouteRecord = { path: string; + alias: Array; regex: RouteRegExp; components: Dictionary; instances: Dictionary; diff --git a/src/create-matcher.js b/src/create-matcher.js index e7d308c7d..9e2f2e649 100644 --- a/src/create-matcher.js +++ b/src/create-matcher.js @@ -13,6 +13,7 @@ export type Matcher = { match: (raw: RawLocation, current?: Route, redirectedFrom?: Location) => Route; addRoutes: (routes: Array) => void; addRoute: (parentNameOrRoute: string | RouteConfig, route?: RouteConfig) => void; + getRoutes: () => Array; }; export function createMatcher ( @@ -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 ( @@ -175,6 +192,7 @@ export function createMatcher ( return { match, addRoute, + getRoutes, addRoutes } } diff --git a/src/create-route-map.js b/src/create-route-map.js index 8d74bcd60..93a558f8a 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -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, diff --git a/src/index.js b/src/index.js index bc6a27585..59fc93788 100644 --- a/src/index.js +++ b/src/index.js @@ -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) { diff --git a/test/unit/specs/create-matcher.spec.js b/test/unit/specs/create-matcher.spec.js index e2caacd76..fcc2587c2 100644 --- a/test/unit/specs/create-matcher.spec.js +++ b/test/unit/specs/create-matcher.spec.js @@ -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])