Skip to content

Commit 4e3b377

Browse files
committed
adjust route component instance registration strategy (fix #1286)
1 parent a62fd6f commit 4e3b377

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/components/view.js

+6-11
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,12 @@ export default {
4545

4646
const component = cache[name] = matched.components[name]
4747

48-
// inject instance registration hooks
49-
const hooks = data.hook || (data.hook = {})
50-
hooks.init = vnode => {
51-
matched.instances[name] = vnode.componentInstance
52-
}
53-
hooks.prepatch = (oldVnode, vnode) => {
54-
matched.instances[name] = vnode.componentInstance
55-
}
56-
hooks.destroy = vnode => {
57-
if (matched.instances[name] === vnode.componentInstance) {
58-
matched.instances[name] = undefined
48+
// attach instance registration hook
49+
// this will be called in the instance's injected lifecycle hooks
50+
data.registerRouteInstance = (vm, val) => {
51+
// val could be undefined for unregistration
52+
if (matched.instances[name] !== vm) {
53+
matched.instances[name] = val
5954
}
6055
}
6156

src/install.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,26 @@ export function install (Vue) {
1717
get () { return this.$root._route }
1818
})
1919

20+
const isDef = v => v !== undefined
21+
22+
const registerInstance = (vm, callVal) => {
23+
let i = vm.$options._parentVnode
24+
if (isDef(i) && isDef(i = i.data) && isDef(i = i.registerRouteInstance)) {
25+
i(vm, callVal)
26+
}
27+
}
28+
2029
Vue.mixin({
2130
beforeCreate () {
22-
if (this.$options.router) {
31+
if (isDef(this.$options.router)) {
2332
this._router = this.$options.router
2433
this._router.init(this)
2534
Vue.util.defineReactive(this, '_route', this._router.history.current)
2635
}
36+
registerInstance(this, this)
37+
},
38+
destroyed () {
39+
registerInstance(this)
2740
}
2841
})
2942

0 commit comments

Comments
 (0)