Skip to content

Commit 94ed276

Browse files
author
Jared Deckard
committed
feat: add a default component for nested routes (vuejs#2105)
1 parent 638278b commit 94ed276

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

examples/nested-routes/app.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,14 @@ const router = new VueRouter({
8080

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

83-
{ name: 'zap', path: 'zap/:zapId?', component: Zap }
83+
{ name: 'zap', path: 'zap/:zapId?', component: Zap },
84+
85+
{
86+
path: 'wrap',
87+
children: [
88+
{ path: 'foo', component: Foo }
89+
]
90+
}
8491
]
8592
}
8693
]
@@ -103,6 +110,7 @@ new Vue({
103110
<li><router-link :to="{ params: { zapId: 2 }}">{ params: { zapId: 2 }} (relative params)</router-link></li>
104111
<li><router-link to="/parent/qux/1/quux">/parent/qux/1/quux</router-link></li>
105112
<li><router-link to="/parent/qux/2/quux">/parent/qux/2/quux</router-link></li>
113+
<li><router-link to="/parent/wrap/foo">/parent/wrap/foo</router-link></li>
106114
</ul>
107115
<router-view class="view"></router-view>
108116
</div>

src/components/pass.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import RouterView from './view'
2+
3+
export default {
4+
name: 'PassThrough',
5+
components: { RouterView },
6+
template: '<router-view />'
7+
}

src/create-route-map.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* @flow */
22

33
import Regexp from 'path-to-regexp'
4+
import PassThrough from './components/pass'
45
import { cleanPath } from './util/path'
56
import { assert, warn } from './util/warn'
67

@@ -109,12 +110,16 @@ function addRouteRecord (
109110
)
110111
}
111112
}
112-
route.children.forEach(child => {
113+
const children = route.children.map(child => {
113114
const childMatchAs = matchAs
114115
? cleanPath(`${matchAs}/${child.path}`)
115116
: undefined
116-
addRouteRecord(pathList, pathMap, nameMap, child, record, childMatchAs)
117+
return addRouteRecord(pathList, pathMap, nameMap, child, record, childMatchAs)
117118
})
119+
// Allow nested child routes to render inside routes without a component.
120+
if (children.some(child => child.components.default)) {
121+
record.components.default = record.components.default || PassThrough
122+
}
118123
}
119124

120125
if (!pathMap[record.path]) {
@@ -161,6 +166,8 @@ function addRouteRecord (
161166
)
162167
}
163168
}
169+
170+
return record
164171
}
165172

166173
function compileRouteRegex (

test/e2e/specs/nested-routes.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
browser
1010
.url('http://localhost:8080/nested-routes/')
1111
.waitForElementVisible('#app', 1000)
12-
.assert.count('li a', 11)
12+
.assert.count('li a', 12)
1313
.assert.urlEquals('http://localhost:8080/nested-routes/parent')
1414
.assert.containsText('.view', 'Parent')
1515
.assert.containsText('.view', 'default')
@@ -99,6 +99,11 @@ module.exports = {
9999
.click('.nested-child a')
100100
.assert.urlEquals('http://localhost:8080/nested-routes/parent/qux/2/quuy')
101101

102+
.click('li:nth-child(12) a')
103+
.assert.urlEquals('http://localhost:8080/nested-routes/parent/wrap/foo')
104+
.assert.containsText('.view', 'Parent')
105+
.assert.containsText('.view', 'foo')
106+
102107
// check initial visit
103108
.url('http://localhost:8080/nested-routes/parent/foo')
104109
.waitForElementVisible('#app', 1000)

0 commit comments

Comments
 (0)