diff --git a/examples/nested-routes/app.js b/examples/nested-routes/app.js index 9f35e3064..926068627 100644 --- a/examples/nested-routes/app.js +++ b/examples/nested-routes/app.js @@ -37,6 +37,7 @@ const Quy = { ` } const Quux = { template: '
quux
' } +const Zap = { template: '

zap

{{ $route.params.zapId }}
' } const router = new VueRouter({ mode: 'history', @@ -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 } ] } ] @@ -85,6 +88,8 @@ new Vue({
  • /baz
  • /parent/qux
  • /parent/quy
  • +
  • /parent/zap
  • +
  • /parent/zap/1
  • diff --git a/src/create-matcher.js b/src/create-matcher.js index 748f451e8..7dbcd1be6 100644 --- a/src/create-matcher.js +++ b/src/create-matcher.js @@ -35,7 +35,10 @@ export function createMatcher (routes: Array): 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 = {} @@ -210,11 +213,6 @@ function fillParams ( } } -function getParams (path: string): Array { - 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) } diff --git a/test/e2e/specs/nested-routes.js b/test/e2e/specs/nested-routes.js index 172ded37e..75c5a4ae8 100644 --- a/test/e2e/specs/nested-routes.js +++ b/test/e2e/specs/nested-routes.js @@ -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') @@ -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)