Skip to content

Fix #1572 router-link in nested component behaves differently than one in a page-route #1918

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ <h1>Vue Router Examples</h1>
<li><a href="nested-routes">Nested Routes</a></li>
<li><a href="named-routes">Named Routes</a></li>
<li><a href="named-views">Named Views</a></li>
<li><a href="nameed-change">Nameed Change</a></li>
<li><a href="route-matching">Route Matching</a></li>
<li><a href="active-links">Active Links</a></li>
<li><a href="redirect">Redirect</a></li>
Expand Down
72 changes: 72 additions & 0 deletions examples/nameed-change/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

let count = 0

const Home = {
template: `
<div>
<span>nav param:</span>
<span class="count" v-text="$route.params.count"></span>
<br>
<button @click="update()">change param</button>

<br>
<br>
<div class="area">
<router-link :to="{ name: 'app' }">
router-link in Home
</router-link>

<br>

<nested-component></nested-component>
</div>
</div>
`,
methods: {
update () {
count++
this.$router.push({
params: {
count: count
}
})
}
}
}

Vue.component('nested-component', {
template: `
<router-link :to="{ name: 'app' }">
router-link in NestedComponent
</router-link>
`
})

const router = new VueRouter({
mode: 'history',
routes: [
{
path: '/nameed-change/:count',
name: 'app',
component: Home
},
{
path: '*',
redirect: { name: 'app', params: { count: 0 }}
}
]
})

new Vue({
router,
template: `
<div id="app">
<router-view></router-view>
</div>
`
}).$mount('#app')

6 changes: 6 additions & 0 deletions examples/nameed-change/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!DOCTYPE html>
<link rel="stylesheet" href="/global.css">
<a href="/">&larr; Examples index</a>
<div id="app"></div>
<script src="/__build__/shared.js"></script>
<script src="/__build__/nameed-change.js"></script>
4 changes: 3 additions & 1 deletion src/components/link.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @flow */

import { createRoute, isSameRoute, isIncludedRoute } from '../util/route'
import { assign } from '../util/location'
import { _Vue } from '../install'

// work around weird flow bug
Expand Down Expand Up @@ -31,7 +32,8 @@ export default {
render (h: Function) {
const router = this.$router
const current = this.$route
const { location, route, href } = router.resolve(this.to, current, this.append)
const to = typeof this.to === 'string' ? this.to : assign({}, this.to) // Fix bug #1572
const { location, route, href } = router.resolve(to, current, this.append)

const classes = {}
const globalActiveClass = router.options.linkActiveClass
Expand Down
2 changes: 1 addition & 1 deletion src/util/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function normalizeLocation (
}
}

function assign (a, b) {
export function assign (a: Object, b: Object): Object {
for (const key in b) {
a[key] = b[key]
}
Expand Down
17 changes: 17 additions & 0 deletions test/e2e/specs/nameed-change.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
'nameed change': function (browser) {
browser
.url('http://localhost:8080/nameed-change/')
.waitForElementVisible('#app', 1000)

.assert.urlEquals('http://localhost:8080/nameed-change/0')
.assert.containsText('.count', '0')
.assert.attributeContains('.area a:nth-child(1)', 'href', '/nameed-change/0')
.assert.attributeContains('.area a:nth-child(1)', 'href', '/nameed-change/0')
.click('button')
.assert.containsText('.count', '1')
.assert.attributeContains('.area a:nth-child(1)', 'href', '/nameed-change/1')
.assert.attributeContains('.area a:nth-child(1)', 'href', '/nameed-change/1')
.end()
}
}