diff --git a/examples/nested-routes/app.js b/examples/nested-routes/app.js index fd26f68ea..6e8ba8689 100644 --- a/examples/nested-routes/app.js +++ b/examples/nested-routes/app.js @@ -19,6 +19,17 @@ const Foo = { template: '
foo
' } const Bar = { template: '
bar
' } const Baz = { template: '
baz
' } +const Qux = { + template: ` +
+

qux

+ /quux + +
+ ` +} +const Quux = { template: '
quux
' } + const router = new VueRouter({ mode: 'history', base: __dirname, @@ -40,7 +51,13 @@ const router = new VueRouter({ // this allows you to leverage the component nesting without being // limited to the nested URL. // components rendered at /baz: Root -> Parent -> Baz - { path: '/baz', component: Baz } + { path: '/baz', component: Baz }, + + { + path: 'qux/:quxId', + component: Qux, + children: [{ path: 'quux', name: 'quux', component: Quux }] + } ] } ] @@ -56,6 +73,7 @@ new Vue({
  • /parent/foo
  • /parent/bar
  • /baz
  • +
  • /parent/qux
  • diff --git a/src/components/link.js b/src/components/link.js index 92ebc310c..5d6ef8b4f 100644 --- a/src/components/link.js +++ b/src/components/link.js @@ -27,7 +27,7 @@ export default { const router = this.$router const current = this.$route const to = normalizeLocation(this.to, current, this.append) - const resolved = router.match(to) + const resolved = router.match(to, current) const fullPath = resolved.redirectedFrom || resolved.fullPath const base = router.history.base const href = createHref(base, fullPath, router.mode) diff --git a/src/create-matcher.js b/src/create-matcher.js index ff6b904a4..1850c5284 100644 --- a/src/create-matcher.js +++ b/src/create-matcher.js @@ -31,6 +31,19 @@ export function createMatcher (routes: Array): Matcher { if (name) { const record = nameMap[name] + + if (typeof location.params !== 'object') { + location.params = {} + } + + if (currentRoute && typeof currentRoute.params === 'object') { + for (const key in currentRoute.params) { + if (!(key in location.params)) { + location.params[key] = currentRoute.params[key] + } + } + } + if (record) { location.path = fillParams(record.path, location.params, `named route "${name}"`) return _createRoute(record, location, redirectedFrom) diff --git a/test/e2e/specs/nested-routes.js b/test/e2e/specs/nested-routes.js index 1284ebab7..41aecaa2d 100644 --- a/test/e2e/specs/nested-routes.js +++ b/test/e2e/specs/nested-routes.js @@ -3,7 +3,7 @@ module.exports = { browser .url('http://localhost:8080/nested-routes/') .waitForElementVisible('#app', 1000) - .assert.count('li a', 4) + .assert.count('li a', 5) .assert.urlEquals('http://localhost:8080/nested-routes/parent') .assert.containsText('.view', 'Parent') .assert.containsText('.view', 'default') @@ -23,6 +23,17 @@ module.exports = { .assert.containsText('.view', 'Parent') .assert.containsText('.view', 'baz') + .click('li:nth-child(5) a') + .assert.urlEquals('http://localhost:8080/nested-routes/parent/qux/123') + .assert.containsText('.view', 'Parent') + .assert.containsText('.view', 'qux') + + .click('.nested-parent a') + .assert.urlEquals('http://localhost:8080/nested-routes/parent/qux/123/quux') + .assert.containsText('.view', 'Parent') + .assert.containsText('.view', 'qux') + .assert.containsText('.view', 'quux') + // check initial visit .url('http://localhost:8080/nested-routes/parent/foo') .waitForElementVisible('#app', 1000)