Skip to content

feat: add a default component for nested routes #2905

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

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 9 additions & 1 deletion examples/nested-routes/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ const router = new VueRouter({

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

{ name: 'zap', path: 'zap/:zapId?', component: Zap }
{ name: 'zap', path: 'zap/:zapId?', component: Zap },

{
path: 'wrap',
children: [
{ path: 'foo', component: Foo }
]
}
]
}
]
Expand All @@ -103,6 +110,7 @@ new Vue({
<li><router-link :to="{ params: { zapId: 2 }}">{ params: { zapId: 2 }} (relative params)</router-link></li>
<li><router-link to="/parent/qux/1/quux">/parent/qux/1/quux</router-link></li>
<li><router-link to="/parent/qux/2/quux">/parent/qux/2/quux</router-link></li>
<li><router-link to="/parent/wrap/foo">/parent/wrap/foo</router-link></li>
</ul>
<router-view class="view"></router-view>
</div>
Expand Down
7 changes: 7 additions & 0 deletions src/components/pass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import RouterView from './view'

export default {
name: 'PassThrough',
components: { RouterView },
template: '<router-view />'
}
11 changes: 9 additions & 2 deletions src/create-route-map.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @flow */

import Regexp from 'path-to-regexp'
import PassThrough from './components/pass'
import { cleanPath } from './util/path'
import { assert, warn } from './util/warn'

Expand Down Expand Up @@ -109,12 +110,16 @@ function addRouteRecord (
)
}
}
route.children.forEach(child => {
const children = route.children.map(child => {
const childMatchAs = matchAs
? cleanPath(`${matchAs}/${child.path}`)
: undefined
addRouteRecord(pathList, pathMap, nameMap, child, record, childMatchAs)
return addRouteRecord(pathList, pathMap, nameMap, child, record, childMatchAs)
})
// Allow nested child routes to render inside routes without a component.
if (children.some(child => child.components.default)) {
record.components.default = record.components.default || PassThrough
}
}

if (!pathMap[record.path]) {
Expand Down Expand Up @@ -161,6 +166,8 @@ function addRouteRecord (
)
}
}

return record
}

function compileRouteRegex (
Expand Down
7 changes: 6 additions & 1 deletion test/e2e/specs/nested-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
browser
.url('http://localhost:8080/nested-routes/')
.waitForElementVisible('#app', 1000)
.assert.count('li a', 11)
.assert.count('li a', 12)
.assert.urlEquals('http://localhost:8080/nested-routes/parent')
.assert.containsText('.view', 'Parent')
.assert.containsText('.view', 'default')
Expand Down Expand Up @@ -99,6 +99,11 @@ module.exports = {
.click('.nested-child a')
.assert.urlEquals('http://localhost:8080/nested-routes/parent/qux/2/quuy')

.click('li:nth-child(12) a')
.assert.urlEquals('http://localhost:8080/nested-routes/parent/wrap/foo')
.assert.containsText('.view', 'Parent')
.assert.containsText('.view', 'foo')

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