Skip to content

Commit 6a18729

Browse files
znckyyx990803
authored andcommitted
Remove extras from old params in named routes (#910)
* 🐛 Remove extra old params in named routes * ✅ Add tests
1 parent d0c21b1 commit 6a18729

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

examples/nested-routes/app.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ const Qux = {
2828
</div>
2929
`
3030
}
31+
const Quy = {
32+
template: `
33+
<div class="nested-parent-other">
34+
<h3>quy</h3>
35+
<pre>{{ JSON.stringify(Object.keys($route.params)) }}</pre>
36+
</div>
37+
`
38+
}
3139
const Quux = { template: '<div>quux</div>' }
3240

3341
const router = new VueRouter({
@@ -57,7 +65,9 @@ const router = new VueRouter({
5765
path: 'qux/:quxId',
5866
component: Qux,
5967
children: [{ path: 'quux', name: 'quux', component: Quux }]
60-
}
68+
},
69+
70+
{ path: 'quy/:quyId', component: Quy }
6171
]
6272
}
6373
]
@@ -74,6 +84,7 @@ new Vue({
7484
<li><router-link to="/parent/bar">/parent/bar</router-link></li>
7585
<li><router-link to="/baz">/baz</router-link></li>
7686
<li><router-link to="/parent/qux/123">/parent/qux</router-link></li>
87+
<li><router-link to="/parent/quy/123">/parent/quy</router-link></li>
7788
</ul>
7889
<router-view class="view"></router-view>
7990
</div>

src/create-matcher.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const regexpCache: {
1414
}
1515
} = Object.create(null)
1616

17+
const regexpParamsCache: {
18+
[key: string]: Array<string>
19+
} = Object.create(null)
20+
1721
const regexpCompileCache: {
1822
[key: string]: Function
1923
} = Object.create(null)
@@ -31,14 +35,15 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {
3135

3236
if (name) {
3337
const record = nameMap[name]
38+
const paramNames = getParams(record.path)
3439

3540
if (typeof location.params !== 'object') {
3641
location.params = {}
3742
}
3843

3944
if (currentRoute && typeof currentRoute.params === 'object') {
4045
for (const key in currentRoute.params) {
41-
if (!(key in location.params)) {
46+
if (!(key in location.params) && paramNames.indexOf(key) > -1) {
4247
location.params[key] = currentRoute.params[key]
4348
}
4449
}
@@ -150,13 +155,10 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {
150155
return match
151156
}
152157

153-
function matchRoute (
154-
path: string,
155-
params: Object,
156-
pathname: string
157-
): boolean {
158-
let keys, regexp
158+
function getRouteRegex (path: string): Object {
159159
const hit = regexpCache[path]
160+
let keys, regexp
161+
160162
if (hit) {
161163
keys = hit.keys
162164
regexp = hit.regexp
@@ -165,6 +167,16 @@ function matchRoute (
165167
regexp = Regexp(path, keys)
166168
regexpCache[path] = { keys, regexp }
167169
}
170+
171+
return { keys, regexp }
172+
}
173+
174+
function matchRoute (
175+
path: string,
176+
params: Object,
177+
pathname: string
178+
): boolean {
179+
const { regexp, keys } = getRouteRegex(path)
168180
const m = pathname.match(regexp)
169181

170182
if (!m) {
@@ -198,6 +210,11 @@ function fillParams (
198210
}
199211
}
200212

213+
function getParams (path: string): Array<string> {
214+
return regexpParamsCache[path] ||
215+
(regexpParamsCache[path] = getRouteRegex(path).keys.map(key => key.name))
216+
}
217+
201218
function resolveRecordPath (path: string, record: RouteRecord): string {
202219
return resolvePath(path, record.parent ? record.parent.path : '/', true)
203220
}

test/e2e/specs/nested-routes.js

+12-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', 5)
6+
.assert.count('li a', 6)
77
.assert.urlEquals('http://localhost:8080/nested-routes/parent')
88
.assert.containsText('.view', 'Parent')
99
.assert.containsText('.view', 'default')
@@ -34,6 +34,17 @@ module.exports = {
3434
.assert.containsText('.view', 'qux')
3535
.assert.containsText('.view', 'quux')
3636

37+
.click('li:nth-child(6) a')
38+
.assert.urlEquals('http://localhost:8080/nested-routes/parent/quy/123')
39+
.assert.containsText('.view', 'Parent')
40+
.assert.containsText('.view', 'quy')
41+
.assert.evaluate(function () {
42+
var params = JSON.parse(document.querySelector('pre').textContent)
43+
return (
44+
JSON.stringify(params) === JSON.stringify(['quyId'])
45+
)
46+
}, null, '/')
47+
3748
// check initial visit
3849
.url('http://localhost:8080/nested-routes/parent/foo')
3950
.waitForElementVisible('#app', 1000)

0 commit comments

Comments
 (0)