Skip to content

Commit 00ea5bb

Browse files
authored
Merge pull request #851 from wjmao88/inherit-params
Allow route to use current params to fill ones it does not provide
2 parents 13e64d6 + 71aaf51 commit 00ea5bb

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

examples/nested-routes/app.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ const Foo = { template: '<div>foo</div>' }
1919
const Bar = { template: '<div>bar</div>' }
2020
const Baz = { template: '<div>baz</div>' }
2121

22+
const Qux = {
23+
template: `
24+
<div class="nested-parent">
25+
<h3>qux</h3>
26+
<router-link :to="{ name: 'quux' }">/quux</router-link>
27+
<router-view class="nested-child"></router-view>
28+
</div>
29+
`
30+
}
31+
const Quux = { template: '<div>quux</div>' }
32+
2233
const router = new VueRouter({
2334
mode: 'history',
2435
base: __dirname,
@@ -40,7 +51,13 @@ const router = new VueRouter({
4051
// this allows you to leverage the component nesting without being
4152
// limited to the nested URL.
4253
// components rendered at /baz: Root -> Parent -> Baz
43-
{ path: '/baz', component: Baz }
54+
{ path: '/baz', component: Baz },
55+
56+
{
57+
path: 'qux/:quxId',
58+
component: Qux,
59+
children: [{ path: 'quux', name: 'quux', component: Quux }]
60+
}
4461
]
4562
}
4663
]
@@ -56,6 +73,7 @@ new Vue({
5673
<li><router-link to="/parent/foo">/parent/foo</router-link></li>
5774
<li><router-link to="/parent/bar">/parent/bar</router-link></li>
5875
<li><router-link to="/baz">/baz</router-link></li>
76+
<li><router-link to="/parent/qux/123">/parent/qux</router-link></li>
5977
</ul>
6078
<router-view class="view"></router-view>
6179
</div>

src/components/link.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export default {
2828
const router = this.$router
2929
const current = this.$route
3030
const to = normalizeLocation(this.to, current, this.append)
31-
const resolved = router.match(to)
31+
const resolved = router.match(to, current)
3232
const fullPath = resolved.redirectedFrom || resolved.fullPath
3333
const base = router.history.base
3434
const href = createHref(base, fullPath, router.mode)

src/create-matcher.js

+13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {
3131

3232
if (name) {
3333
const record = nameMap[name]
34+
35+
if (typeof location.params !== 'object') {
36+
location.params = {}
37+
}
38+
39+
if (currentRoute && typeof currentRoute.params === 'object') {
40+
for (const key in currentRoute.params) {
41+
if (!(key in location.params)) {
42+
location.params[key] = currentRoute.params[key]
43+
}
44+
}
45+
}
46+
3447
if (record) {
3548
location.path = fillParams(record.path, location.params, `named route "${name}"`)
3649
return _createRoute(record, location, redirectedFrom)

test/e2e/specs/nested-routes.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = {
33
browser
44
.url('http://localhost:8080/nested-routes/')
55
.waitForElementVisible('#app', 1000)
6-
.assert.count('li a', 4)
6+
.assert.count('li a', 5)
77
.assert.urlEquals('http://localhost:8080/nested-routes/parent')
88
.assert.containsText('.view', 'Parent')
99
.assert.containsText('.view', 'default')
@@ -23,6 +23,17 @@ module.exports = {
2323
.assert.containsText('.view', 'Parent')
2424
.assert.containsText('.view', 'baz')
2525

26+
.click('li:nth-child(5) a')
27+
.assert.urlEquals('http://localhost:8080/nested-routes/parent/qux/123')
28+
.assert.containsText('.view', 'Parent')
29+
.assert.containsText('.view', 'qux')
30+
31+
.click('.nested-parent a')
32+
.assert.urlEquals('http://localhost:8080/nested-routes/parent/qux/123/quux')
33+
.assert.containsText('.view', 'Parent')
34+
.assert.containsText('.view', 'qux')
35+
.assert.containsText('.view', 'quux')
36+
2637
// check initial visit
2738
.url('http://localhost:8080/nested-routes/parent/foo')
2839
.waitForElementVisible('#app', 1000)

0 commit comments

Comments
 (0)