Skip to content

Commit aad2ed6

Browse files
committed
support nested children for aliased paths (fix #987)
1 parent e8ae253 commit aad2ed6

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

examples/route-alias/app.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const Foo = { template: '<div>foo</div>' }
88
const Bar = { template: '<div>bar</div>' }
99
const Baz = { template: '<div>baz</div>' }
1010
const Default = { template: '<div>default</div>' }
11+
const Nested = { template: '<router-view/>' }
12+
const NestedFoo = { template: '<div>nested foo</div>' }
1113

1214
const router = new VueRouter({
1315
mode: 'history',
@@ -22,7 +24,13 @@ const router = new VueRouter({
2224
// multiple aliases
2325
{ path: 'baz', component: Baz, alias: ['/baz', 'baz-alias'] },
2426
// default child route with empty string as alias.
25-
{ path: 'default', component: Default, alias: '' }
27+
{ path: 'default', component: Default, alias: '' },
28+
// nested alias
29+
{ path: 'nested', component: Nested, alias: 'nested-alias',
30+
children: [
31+
{ path: 'foo', component: NestedFoo }
32+
]
33+
}
2634
]
2735
}
2836
]
@@ -53,6 +61,10 @@ new Vue({
5361
<li><router-link to="/home">
5462
/home (renders /home/default)
5563
</router-link></li>
64+
65+
<li><router-link to="/home/nested-alias/foo">
66+
/home/nested-alias/foo (renders /home/nested/foo)
67+
</router-link></li>
5668
</ul>
5769
<router-view class="view"></router-view>
5870
</div>

src/create-route-map.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,35 @@ function addRouteRecord (
6666
}
6767
}
6868
route.children.forEach(child => {
69-
addRouteRecord(pathMap, nameMap, child, record)
69+
const childMatchAs = matchAs
70+
? cleanPath(`${matchAs}/${child.path}`)
71+
: undefined
72+
addRouteRecord(pathMap, nameMap, child, record, childMatchAs)
7073
})
7174
}
7275

7376
if (route.alias !== undefined) {
7477
if (Array.isArray(route.alias)) {
7578
route.alias.forEach(alias => {
76-
addRouteRecord(pathMap, nameMap, { path: alias }, parent, record.path)
79+
const aliasRoute = {
80+
path: alias,
81+
children: route.children
82+
}
83+
addRouteRecord(pathMap, nameMap, aliasRoute, parent, record.path)
7784
})
7885
} else {
79-
addRouteRecord(pathMap, nameMap, { path: route.alias }, parent, record.path)
86+
const aliasRoute = {
87+
path: route.alias,
88+
children: route.children
89+
}
90+
addRouteRecord(pathMap, nameMap, aliasRoute, parent, record.path)
8091
}
8192
}
8293

8394
if (!pathMap[record.path]) {
8495
pathMap[record.path] = record
8596
}
97+
8698
if (name) {
8799
if (!nameMap[name]) {
88100
nameMap[name] = record

test/e2e/specs/route-alias.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ module.exports = {
33
browser
44
.url('http://localhost:8080/route-alias/')
55
.waitForElementVisible('#app', 1000)
6-
.assert.count('li a', 5)
6+
.assert.count('li a', 6)
77
// assert correct href with base
88
.assert.attributeContains('li:nth-child(1) a', 'href', '/route-alias/foo')
99
.assert.attributeContains('li:nth-child(2) a', 'href', '/route-alias/home/bar-alias')
1010
.assert.attributeContains('li:nth-child(3) a', 'href', '/route-alias/baz')
1111
.assert.attributeContains('li:nth-child(4) a', 'href', '/route-alias/home/baz-alias')
1212
.assert.attributeEquals('li:nth-child(5) a', 'href', 'http://localhost:8080/route-alias/home')
13+
.assert.attributeContains('li:nth-child(6) a', 'href', '/route-alias/home/nested-alias/foo')
1314

1415
.click('li:nth-child(1) a')
1516
.assert.urlEquals('http://localhost:8080/route-alias/foo')
@@ -30,11 +31,17 @@ module.exports = {
3031
.assert.urlEquals('http://localhost:8080/route-alias/home/baz-alias')
3132
.assert.containsText('.view', 'Home')
3233
.assert.containsText('.view', 'baz')
34+
3335
.click('li:nth-child(5) a')
3436
.assert.urlEquals('http://localhost:8080/route-alias/home')
3537
.assert.containsText('.view', 'Home')
3638
.assert.containsText('.view', 'default')
3739

40+
.click('li:nth-child(6) a')
41+
.assert.urlEquals('http://localhost:8080/route-alias/home/nested-alias/foo')
42+
.assert.containsText('.view', 'Home')
43+
.assert.containsText('.view', 'nested foo')
44+
3845
// check initial visit
3946
.url('http://localhost:8080/route-alias/foo')
4047
.waitForElementVisible('#app', 1000)
@@ -65,6 +72,12 @@ module.exports = {
6572
.assert.urlEquals('http://localhost:8080/route-alias/home')
6673
.assert.containsText('.view', 'Home')
6774
.assert.containsText('.view', 'default')
75+
76+
.url('http://localhost:8080/route-alias/home/nested-alias/foo')
77+
.waitForElementVisible('#app', 1000)
78+
.assert.urlEquals('http://localhost:8080/route-alias/home/nested-alias/foo')
79+
.assert.containsText('.view', 'Home')
80+
.assert.containsText('.view', 'nested foo')
6881
.end()
6982
}
7083
}

0 commit comments

Comments
 (0)