Skip to content

Don't use old optional params in routes #917

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

Merged
merged 2 commits into from
Nov 23, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion examples/nested-routes/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const Quy = {
`
}
const Quux = { template: '<div>quux</div>' }
const Zap = { template: '<div><h3>zap</h3><pre>{{ $route.params.zapId }}</pre></div>' }

const router = new VueRouter({
mode: 'history',
Expand Down Expand Up @@ -67,7 +68,9 @@ const router = new VueRouter({
children: [{ path: 'quux', name: 'quux', component: Quux }]
},

{ path: 'quy/:quyId', component: Quy }
{ path: 'quy/:quyId', component: Quy },

{ name: 'zap', path: 'zap/:zapId?', component: Zap }
]
}
]
Expand All @@ -85,6 +88,8 @@ new Vue({
<li><router-link to="/baz">/baz</router-link></li>
<li><router-link to="/parent/qux/123">/parent/qux</router-link></li>
<li><router-link to="/parent/quy/123">/parent/quy</router-link></li>
<li><router-link :to="{name: 'zap'}">/parent/zap</router-link></li>
<li><router-link :to="{name: 'zap', params: {zapId: 1}}">/parent/zap/1</router-link></li>
</ul>
<router-view class="view"></router-view>
</div>
Expand Down
4 changes: 3 additions & 1 deletion src/create-matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ function fillParams (

function getParams (path: string): Array<string> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably should rename it to getRequiredParams to avoid ambiguity,
or maybe remove this function and use inline expression at https://github.com/znck/vue-router/blob/c3ec53191647d5a4dfb7481ea3c4d088f2c6c6d7/src/create-matcher.js#L38

return regexpParamsCache[path] ||
(regexpParamsCache[path] = getRouteRegex(path).keys.map(key => key.name))
(regexpParamsCache[path] = getRouteRegex(path).keys
.filter(key => !key.optional)
.map(key => key.name))
}

function resolveRecordPath (path: string, record: RouteRecord): string {
Expand Down
20 changes: 19 additions & 1 deletion test/e2e/specs/nested-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
browser
.url('http://localhost:8080/nested-routes/')
.waitForElementVisible('#app', 1000)
.assert.count('li a', 6)
.assert.count('li a', 8)
.assert.urlEquals('http://localhost:8080/nested-routes/parent')
.assert.containsText('.view', 'Parent')
.assert.containsText('.view', 'default')
Expand Down Expand Up @@ -45,6 +45,24 @@ module.exports = {
)
}, null, '/')

.click('li:nth-child(8) a')
.assert.urlEquals('http://localhost:8080/nested-routes/parent/zap/1')
.assert.containsText('.view', 'Parent')
.assert.containsText('.view', 'zap')
.assert.evaluate(function () {
var zapId = document.querySelector('pre').textContent
return (zapId === '1')
}, null, '/')

.click('li:nth-child(7) a')
.assert.urlEquals('http://localhost:8080/nested-routes/parent/zap')
.assert.containsText('.view', 'Parent')
.assert.containsText('.view', 'zap')
.assert.evaluate(function () {
var zapId = document.querySelector('pre').textContent
return (zapId === '')
}, null, '/')

// check initial visit
.url('http://localhost:8080/nested-routes/parent/foo')
.waitForElementVisible('#app', 1000)
Expand Down