Skip to content

Commit

Permalink
feat: addRoute as nested route
Browse files Browse the repository at this point in the history
Close #1156
  • Loading branch information
posva committed Dec 29, 2020
1 parent d80bcdf commit ca80c44
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/create-matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { decode } from './util/query'
export type Matcher = {
match: (raw: RawLocation, current?: Route, redirectedFrom?: Location) => Route;
addRoutes: (routes: Array<RouteConfig>) => void;
addRoute: (parentNameOrRoute: string | RouteConfig, route?: RouteConfig) => void;
};

export function createMatcher (
Expand All @@ -24,6 +25,12 @@ export function createMatcher (
createRouteMap(routes, pathList, pathMap, nameMap)
}

function addRoute (parentOrRoute, route) {
const parent = (typeof parentOrRoute !== 'object') ? nameMap[parentOrRoute] : undefined
// $flow-disable-line
createRouteMap([route || parentOrRoute], pathList, pathMap, nameMap, parent)
}

function match (
raw: RawLocation,
currentRoute?: Route,
Expand Down Expand Up @@ -167,6 +174,7 @@ export function createMatcher (

return {
match,
addRoute,
addRoutes
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/create-route-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export function createRouteMap (
routes: Array<RouteConfig>,
oldPathList?: Array<string>,
oldPathMap?: Dictionary<RouteRecord>,
oldNameMap?: Dictionary<RouteRecord>
oldNameMap?: Dictionary<RouteRecord>,
parentRoute?: RouteRecord
): {
pathList: Array<string>,
pathMap: Dictionary<RouteRecord>,
Expand All @@ -22,7 +23,7 @@ export function createRouteMap (
const nameMap: Dictionary<RouteRecord> = oldNameMap || Object.create(null)

routes.forEach(route => {
addRouteRecord(pathList, pathMap, nameMap, route)
addRouteRecord(pathList, pathMap, nameMap, route, parentRoute)
})

// ensure wildcard routes are always at the end
Expand Down
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ export default class VueRouter {
}
}

addRoute (parentOrRoute: string | RouteConfig, route?: RouteConfig) {
this.matcher.addRoute(parentOrRoute, route)
if (this.history.current !== START) {
this.history.transitionTo(this.history.getCurrentLocation())
}
}

addRoutes (routes: Array<RouteConfig>) {
this.matcher.addRoutes(routes)
if (this.history.current !== START) {
Expand Down
32 changes: 32 additions & 0 deletions test/unit/specs/create-matcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,38 @@ describe('Creating Matcher', function () {
process.env.NODE_ENV = 'production'
})

it('can add nested routes', function () {
const component = { name: 'fake' }
const matcher = createMatcher([
{
path: '/p',
name: 'parent',
children: [
{ path: 'a', name: 'a', component },
{
path: 'c',
name: 'child',
component,
children: [{ path: 'n', name: 'nested', component }]
}
]
},
{
// easier to debug tests
path: '*', name: 'not-found', component
}
])

matcher.addRoute({ path: '/b', name: 'b', component })
matcher.addRoute('parent', { path: 'b', name: 'p-b', component })
matcher.addRoute('child', { path: 'b', name: 'p-c-b', component })

expect(matcher.match('/b').name).toBe('b')
expect(matcher.match('/p/b').name).toBe('p-b')
expect(matcher.match('/p/c/b').name).toBe('p-c-b')
expect(matcher.match('/p/c/n').name).toBe('nested')
})

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

1 comment on commit ca80c44

@vskavgaci
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Eduardo, thanks for this feature. is this feature gonna be release in soon?

Please sign in to comment.