Skip to content

Commit bf74e6d

Browse files
committed
expose this.$router
1 parent 1beadd2 commit bf74e6d

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,12 @@ class Router {
435435

436436
if (!this.app) {
437437
// initial render
438+
const router = this
438439
this.app = new this._appConstructor({
439440
el: this._appContainer,
441+
created () {
442+
this.$router = router
443+
},
440444
_meta: {
441445
$route: route
442446
}

src/override.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
export default function (Vue) {
22

3-
const _ = Vue.util
3+
const {
4+
extend,
5+
isArray,
6+
defineReactive
7+
} = Vue.util
48

59
// override Vue's init and destroy process to keep track of router instances
610
const init = Vue.prototype._init
711
Vue.prototype._init = function (options) {
812
options = options || {}
913
const root = options._parent || options.parent || this
14+
const router = root.$router
1015
const route = root.$route
11-
if (route) {
12-
route.router._children.push(this)
13-
if (!this.$route) {
14-
/* istanbul ignore if */
15-
if (this._defineMeta) {
16-
// 0.12
17-
this._defineMeta('$route', route)
18-
} else {
19-
// 1.0
20-
_.defineReactive(this, '$route', route)
21-
}
16+
if (router) {
17+
// expose router
18+
this.$router = router
19+
router._children.push(this)
20+
/* istanbul ignore if */
21+
if (this._defineMeta) {
22+
// 0.12
23+
this._defineMeta('$route', route)
24+
} else {
25+
// 1.0
26+
defineReactive(this, '$route', route)
2227
}
2328
}
2429
init.call(this, options)
@@ -27,9 +32,8 @@ export default function (Vue) {
2732
const destroy = Vue.prototype._destroy
2833
Vue.prototype._destroy = function () {
2934
if (!this._isBeingDestroyed) {
30-
const route = this.$root.$route
31-
if (route) {
32-
route.router._children.$remove(this)
35+
if (this.$router) {
36+
this.$router._children.$remove(this)
3337
}
3438
destroy.apply(this, arguments)
3539
}
@@ -44,14 +48,14 @@ export default function (Vue) {
4448
if (!childVal) return parentVal
4549
if (!parentVal) return childVal
4650
const ret = {}
47-
_.extend(ret, parentVal)
51+
extend(ret, parentVal)
4852
for (let key in childVal) {
4953
let a = ret[key]
5054
let b = childVal[key]
5155
// for data, activate and deactivate, we need to merge them into
5256
// arrays similar to lifecycle hooks.
5357
if (a && hooksToMergeRE.test(key)) {
54-
ret[key] = (_.isArray(a) ? a : [a]).concat(b)
58+
ret[key] = (isArray(a) ? a : [a]).concat(b)
5559
} else {
5660
ret[key] = b
5761
}

0 commit comments

Comments
 (0)