Skip to content

Commit 53cce99

Browse files
authored
fix(normalizeLocation): create a copy with named locations (#2286)
* fix(normalizeLocation): create a copy with non normalized named locations Fix #2121 * test(nested-routes): add e2e test for implicit params * chore(lint): fix
1 parent e57882c commit 53cce99

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

examples/nested-routes/app.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,22 @@ const Quy = {
3636
</div>
3737
`
3838
}
39-
const Quux = { template: '<div>quux</div>' }
40-
const Zap = { template: '<div><h3>zap</h3><pre>{{ $route.params.zapId }}</pre></div>' }
39+
const Quux = {
40+
template: `<div>quux<router-link :to="{ name: 'quuy' }">go to quuy</router-link></div>`
41+
}
42+
const Quuy = { template: '<div>quuy</div>' }
43+
const Zap = {
44+
template: '<div><h3>zap</h3><pre>{{ $route.params.zapId }}</pre></div>'
45+
}
4146

4247
const router = new VueRouter({
4348
mode: 'history',
4449
base: __dirname,
4550
routes: [
4651
{ path: '/', redirect: '/parent' },
47-
{ path: '/parent', component: Parent,
52+
{
53+
path: '/parent',
54+
component: Parent,
4855
children: [
4956
// an empty path will be treated as the default, e.g.
5057
// components rendered at /parent: Root -> Parent -> Default
@@ -65,7 +72,10 @@ const router = new VueRouter({
6572
{
6673
path: 'qux/:quxId',
6774
component: Qux,
68-
children: [{ path: 'quux', name: 'quux', component: Quux }]
75+
children: [
76+
{ path: 'quux', name: 'quux', component: Quux },
77+
{ path: 'quuy', name: 'quuy', component: Quuy }
78+
]
6979
},
7080

7181
{ path: 'quy/:quyId', component: Quy },
@@ -91,6 +101,8 @@ new Vue({
91101
<li><router-link :to="{name: 'zap'}">/parent/zap</router-link></li>
92102
<li><router-link :to="{name: 'zap', params: {zapId: 1}}">/parent/zap/1</router-link></li>
93103
<li><router-link :to="{ params: { zapId: 2 }}">{ params: { zapId: 2 }} (relative params)</router-link></li>
104+
<li><router-link to="/parent/qux/1/quux">/parent/qux/1/quux</router-link></li>
105+
<li><router-link to="/parent/qux/2/quux">/parent/qux/2/quux</router-link></li>
94106
</ul>
95107
<router-view class="view"></router-view>
96108
</div>

src/util/location.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ export function normalizeLocation (
1515
): Location {
1616
let next: Location = typeof raw === 'string' ? { path: raw } : raw
1717
// named target
18-
if (next.name || next._normalized) {
18+
if (next._normalized) {
1919
return next
20+
} else if (next.name) {
21+
return extend({}, raw)
2022
}
2123

2224
// relative params

test/e2e/specs/nested-routes.js

+8-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', 9)
6+
.assert.count('li a', 11)
77
.assert.urlEquals('http://localhost:8080/nested-routes/parent')
88
.assert.containsText('.view', 'Parent')
99
.assert.containsText('.view', 'default')
@@ -70,6 +70,13 @@ module.exports = {
7070
return (zapId === '2')
7171
}, null, 'relative params')
7272

73+
.click('li:nth-child(10) a')
74+
.assert.urlEquals('http://localhost:8080/nested-routes/parent/qux/1/quux')
75+
.click('li:nth-child(11) a')
76+
.assert.urlEquals('http://localhost:8080/nested-routes/parent/qux/2/quux')
77+
.click('.nested-child a')
78+
.assert.urlEquals('http://localhost:8080/nested-routes/parent/qux/2/quuy')
79+
7380
// check initial visit
7481
.url('http://localhost:8080/nested-routes/parent/foo')
7582
.waitForElementVisible('#app', 1000)

test/unit/specs/location.spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,14 @@ describe('Location utils', () => {
120120
const loc2 = normalizeLocation(loc1)
121121
expect(loc1).toBe(loc2)
122122
})
123+
124+
it('creates copies when not normalized', () => {
125+
const l1 = { name: 'foo' }
126+
expect(normalizeLocation(l1)).not.toBe(l1)
127+
const l2 = { path: '/foo' }
128+
expect(normalizeLocation(l2)).not.toBe(l2)
129+
const l3 = { path: '/foo', query: { foo: 'foo' }}
130+
expect(normalizeLocation(l3)).not.toBe(l3)
131+
})
123132
})
124133
})

0 commit comments

Comments
 (0)