Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 4 additions & 6 deletions src/create-matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {

if (name) {
const record = nameMap[name]
const paramNames = getParams(record.path)
const paramNames = regexpParamsCache[record.path] ||
(regexpParamsCache[record.path] = getRouteRegex(record.path).keys
.filter(key => !key.optional)
.map(key => key.name))

if (typeof location.params !== 'object') {
location.params = {}
Expand Down Expand Up @@ -210,11 +213,6 @@ function fillParams (
}
}

function getParams (path: string): Array<string> {
return regexpParamsCache[path] ||
(regexpParamsCache[path] = getRouteRegex(path).keys.map(key => key.name))
}

function resolveRecordPath (path: string, record: RouteRecord): string {
return resolvePath(path, record.parent ? record.parent.path : '/', true)
}
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