Skip to content

Commit 759df36

Browse files
committed
support nested router (close #1476)
1 parent 8e30c34 commit 759df36

File tree

6 files changed

+103
-9
lines changed

6 files changed

+103
-9
lines changed

Diff for: examples/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ <h1>Vue Router Examples</h1>
2424
<li><a href="lazy-loading">Lazy Loading</a></li>
2525
<li><a href="auth-flow">Auth Flow</a></li>
2626
<li><a href="discrete-components">Discrete Components</a></li>
27+
<li><a href="nested-router">Nested Routers</a></li>
2728
</ul>
2829
</body>
2930
</html>

Diff for: examples/nested-router/app.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import Vue from 'vue'
2+
import VueRouter from 'vue-router'
3+
4+
Vue.use(VueRouter)
5+
6+
const Foo = { template: `<div class="foo"><h1>foo</h1></div>` }
7+
const Bar = { template: `<div class="bar"><h1>bar</h1></div>` }
8+
9+
const childRouter = new VueRouter({
10+
mode: 'abstract',
11+
routes: [
12+
{ path: '/foo', component: Foo },
13+
{ path: '/bar', component: Bar }
14+
]
15+
})
16+
17+
const Nested = {
18+
router: childRouter,
19+
template: `<div class="child">
20+
<p>Child router path: {{ $route.fullPath }}</p>
21+
<ul>
22+
<li><router-link to="/foo">/foo</router-link></li>
23+
<li><router-link to="/bar">/bar</router-link></li>
24+
</ul>
25+
<router-view/>
26+
</div>`
27+
}
28+
29+
const router = new VueRouter({
30+
mode: 'history',
31+
base: __dirname,
32+
routes: [
33+
{ path: '/nested-router', component: Nested },
34+
{ path: '/foo', component: Foo },
35+
{ path: '/bar', component: Bar }
36+
]
37+
})
38+
39+
new Vue({
40+
router,
41+
template: `
42+
<div id="app">
43+
<p>Root router path: {{ $route.fullPath }}</p>
44+
<ul>
45+
<li><router-link to="/nested-router">/nested-router</router-link></li>
46+
<li><router-link to="/foo">/foo</router-link></li>
47+
<li><router-link to="/bar">/bar</router-link></li>
48+
</ul>
49+
<router-view></router-view>
50+
</div>
51+
`
52+
}).$mount('#app')

Diff for: examples/nested-router/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<link rel="stylesheet" href="/global.css">
3+
<style>
4+
#app { border: 1px solid red; }
5+
.child { border: 1px solid blue; padding: 0 15px; margin: 15px; }
6+
</style>
7+
<a href="/">&larr; Examples index</a>
8+
<div id="app"></div>
9+
<script src="/__build__/shared.js"></script>
10+
<script src="/__build__/nested-router.js"></script>

Diff for: src/components/view.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default {
2323
// has been toggled inactive but kept-alive.
2424
let depth = 0
2525
let inactive = false
26-
while (parent) {
26+
while (parent && !parent._routerRoot) {
2727
if (parent.$vnode && parent.$vnode.data.routerView) {
2828
depth++
2929
}

Diff for: src/install.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ export function install (Vue) {
99

1010
_Vue = Vue
1111

12-
Object.defineProperty(Vue.prototype, '$router', {
13-
get () { return this.$root._router }
14-
})
15-
16-
Object.defineProperty(Vue.prototype, '$route', {
17-
get () { return this.$root._route }
18-
})
19-
2012
const isDef = v => v !== undefined
2113

2214
const registerInstance = (vm, callVal) => {
@@ -29,9 +21,12 @@ export function install (Vue) {
2921
Vue.mixin({
3022
beforeCreate () {
3123
if (isDef(this.$options.router)) {
24+
this._routerRoot = this
3225
this._router = this.$options.router
3326
this._router.init(this)
3427
Vue.util.defineReactive(this, '_route', this._router.history.current)
28+
} else {
29+
this._routerRoot = (this.$parent && this.$parent._routerRoot) || this
3530
}
3631
registerInstance(this, this)
3732
},
@@ -40,6 +35,14 @@ export function install (Vue) {
4035
}
4136
})
4237

38+
Object.defineProperty(Vue.prototype, '$router', {
39+
get () { return this._routerRoot._router }
40+
})
41+
42+
Object.defineProperty(Vue.prototype, '$route', {
43+
get () { return this._routerRoot._route }
44+
})
45+
4346
Vue.component('router-view', View)
4447
Vue.component('router-link', Link)
4548

Diff for: test/e2e/specs/nested-router.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module.exports = {
2+
'basic': function (browser) {
3+
browser
4+
.url('http://localhost:8080/nested-router/')
5+
.waitForElementVisible('#app', 1000)
6+
.assert.count('li a', 3)
7+
8+
.click('li:nth-child(1) a')
9+
.assert.urlEquals('http://localhost:8080/nested-router/nested-router')
10+
.assert.containsText('.child', 'Child router path: /')
11+
.assert.count('li a', 5)
12+
13+
.click('.child li:nth-child(1) a')
14+
.assert.containsText('.child', 'Child router path: /foo')
15+
.assert.containsText('.child .foo', 'foo')
16+
17+
.click('.child li:nth-child(2) a')
18+
.assert.containsText('.child', 'Child router path: /bar')
19+
.assert.containsText('.child .bar', 'bar')
20+
21+
.click('li:nth-child(2) a')
22+
.assert.urlEquals('http://localhost:8080/nested-router/foo')
23+
.assert.elementNotPresent('.child')
24+
.assert.containsText('#app', 'foo')
25+
.assert.count('li a', 3)
26+
.end()
27+
}
28+
}

0 commit comments

Comments
 (0)