Skip to content

Allow route to use current params to fill ones it does not provide #851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion examples/nested-routes/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
const Baz = { template: '<div>baz</div>' }

const Qux = {
template: `
<div class="nested-parent">
<h3>qux</h3>
<router-link :to="{ name: 'quux' }">/quux</router-link>
<router-view class="nested-child"></router-view>
</div>
`
}
const Quux = { template: '<div>quux</div>' }

const router = new VueRouter({
mode: 'history',
base: __dirname,
Expand All @@ -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 }]
}
]
}
]
Expand All @@ -56,6 +73,7 @@ new Vue({
<li><router-link to="/parent/foo">/parent/foo</router-link></li>
<li><router-link to="/parent/bar">/parent/bar</router-link></li>
<li><router-link to="/baz">/baz</router-link></li>
<li><router-link to="/parent/qux/123">/parent/qux</router-link></li>
</ul>
<router-view class="view"></router-view>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/components/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions src/create-matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ export function createMatcher (routes: Array<RouteConfig>): 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)
Expand Down
13 changes: 12 additions & 1 deletion test/e2e/specs/nested-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand Down