Skip to content

Commit c8ebba8

Browse files
committed
ensure $route is available in data() (fix #177)
1 parent 1f84077 commit c8ebba8

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

src/mixin.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,45 @@ export default function (Vue) {
22

33
const _ = Vue.util
44
const mixin = {
5-
created: function () {
6-
var route = this.$root.$route
5+
init: function () {
6+
const route = this.$root.$route
77
if (route) {
88
route.router._children.push(this)
99
if (!this.$route) {
10-
if (this._defineMeta) {
11-
// 0.12 compat
12-
this._defineMeta('$route', route)
13-
} else {
14-
_.defineReactive(this, '$route', route)
15-
}
10+
_.defineReactive(this, '$route', route)
1611
}
1712
}
1813
},
1914
beforeDestroy: function () {
20-
var route = this.$root.$route
15+
const route = this.$root.$route
2116
if (route) {
2217
route.router._children.$remove(this)
2318
}
2419
}
2520
}
2621

22+
// pre 1.0.0-rc compat
23+
if (!Vue.config.optionMergeStrategies ||
24+
!Vue.config.optionMergeStrategies.init) {
25+
delete mixin.init
26+
const init = Vue.prototype._init
27+
Vue.prototype._init = function (options) {
28+
const root = options._parent || options.parent || this
29+
const route = root.$route
30+
if (route) {
31+
route.router._children.push(this)
32+
if (!this.$route) {
33+
if (this._defineMeta) {
34+
this._defineMeta('$route', route)
35+
} else {
36+
_.defineReactive(this, '$route', route)
37+
}
38+
}
39+
}
40+
init.call(this, options)
41+
}
42+
}
43+
2744
if (Vue.mixin) {
2845
Vue.mixin(mixin)
2946
} else {

0 commit comments

Comments
 (0)