Skip to content

Commit 9fe3cef

Browse files
zrh122posva
authored andcommitted
fix(normalizeLocation): add a copy for params with named locations
1 parent 7f58509 commit 9fe3cef

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

Diff for: examples/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ <h1>Vue Router Examples</h1>
1717
<li><a href="redirect">Redirect</a></li>
1818
<li><a href="route-props">Route Props</a></li>
1919
<li><a href="route-alias">Route Alias</a></li>
20+
<li><a href="route-params">Route Params</a></li>
2021
<li><a href="transitions">Transitions</a></li>
2122
<li><a href="data-fetching">Data Fetching</a></li>
2223
<li><a href="navigation-guards">Navigation Guards</a></li>

Diff for: examples/route-params/app.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import Vue from 'vue'
2+
import VueRouter from 'vue-router'
3+
4+
Vue.use(VueRouter)
5+
6+
const Log = {
7+
template: `<div class="log">id: {{$route.params.id}}, type: {{$route.params.type}}</div>`
8+
}
9+
10+
const Logs = {
11+
template: `
12+
<div>
13+
<router-link :to="to" class="child-link">{{to.params.type}}</router-link>
14+
<router-view></router-view>
15+
</div>
16+
`,
17+
data () {
18+
return {
19+
to: {
20+
name: 'items.logs.type',
21+
params: { type: 'info' }
22+
}
23+
}
24+
}
25+
}
26+
27+
const router = new VueRouter({
28+
mode: 'history',
29+
base: __dirname,
30+
routes: [
31+
{
32+
path: '/items/:id/logs',
33+
component: Logs,
34+
children: [
35+
{
36+
path: ':type',
37+
name: 'items.logs.type',
38+
component: Log
39+
}
40+
]
41+
}
42+
]
43+
})
44+
45+
new Vue({
46+
router,
47+
template: `
48+
<div id="app">
49+
<h1>Route params</h1>
50+
<ul>
51+
<li><router-link to="/items/1/logs">item #1</router-link></li>
52+
<li><router-link to="/items/2/logs">item #2</router-link></li>
53+
</ul>
54+
<router-view class="view"></router-view>
55+
</div>
56+
`
57+
}).$mount('#app')

Diff for: examples/route-params/index.html

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!DOCTYPE html>
2+
<link rel="stylesheet" href="/global.css">
3+
<a href="/">&larr; Examples index</a>
4+
<div id="app"></div>
5+
<script src="/__build__/shared.chunk.js"></script>
6+
<script src="/__build__/route-params.js"></script>

Diff for: src/util/location.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ export function normalizeLocation (
1818
if (next._normalized) {
1919
return next
2020
} else if (next.name) {
21-
return extend({}, raw)
21+
next = extend({}, raw)
22+
const params = next.params
23+
if (params && typeof params === 'object') {
24+
next.params = extend({}, params)
25+
}
26+
return next
2227
}
2328

2429
// relative params

Diff for: test/e2e/specs/route-params.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module.exports = {
2+
'route-params': function (browser) {
3+
browser
4+
.url('http://localhost:8080/route-params/')
5+
.waitForElementVisible('#app', 1000)
6+
.assert.count('li a', 2)
7+
8+
// https://github.com/vuejs/vue-router/issues/2800
9+
.click('li:nth-child(1) a')
10+
.assert.urlEquals('http://localhost:8080/route-params/items/1/logs')
11+
.click('.child-link')
12+
.assert.urlEquals('http://localhost:8080/route-params/items/1/logs/info')
13+
.assert.containsText('.log', 'id: 1, type: info')
14+
15+
.click('li:nth-child(2) a')
16+
.assert.urlEquals('http://localhost:8080/route-params/items/2/logs')
17+
.click('.child-link')
18+
.assert.urlEquals('http://localhost:8080/route-params/items/2/logs/info')
19+
.assert.containsText('.log', 'id: 2, type: info')
20+
21+
.end()
22+
}
23+
}

0 commit comments

Comments
 (0)