Skip to content

Commit 2b39f5a

Browse files
zrh122posva
authored andcommitted
fix(location): add a copy for params with named locations (#2802)
Closes #2800, #2938 * fix(normalizeLocation): add a copy for params with named locations * test: add test case for #2938
1 parent 7f58509 commit 2b39f5a

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

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>

examples/route-params/app.js

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

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>

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

test/e2e/specs/route-params.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
.assert.containsText('#params', JSON.stringify({ type: 'info' }, null, 2))
12+
.click('.child-link')
13+
.assert.urlEquals('http://localhost:8080/route-params/items/1/logs/info')
14+
.assert.containsText('.log', 'id: 1, type: info')
15+
// https://github.com/vuejs/vue-router/issues/2938
16+
.assert.containsText('#params', JSON.stringify({ type: 'info' }, null, 2))
17+
18+
.click('li:nth-child(2) a')
19+
.assert.urlEquals('http://localhost:8080/route-params/items/2/logs')
20+
.click('.child-link')
21+
.assert.urlEquals('http://localhost:8080/route-params/items/2/logs/info')
22+
.assert.containsText('.log', 'id: 2, type: info')
23+
24+
.end()
25+
}
26+
}

0 commit comments

Comments
 (0)