diff --git a/src/core/observer/dep.js b/src/core/observer/dep.js index 1dd37783..d344aff9 100644 --- a/src/core/observer/dep.js +++ b/src/core/observer/dep.js @@ -55,11 +55,12 @@ export default class Dep { Dep.target = null const targetStack = [] -export function pushTarget (_target: ?Watcher) { - if (Dep.target) targetStack.push(Dep.target) - Dep.target = _target +export function pushTarget (target: ?Watcher) { + targetStack.push(target) + Dep.target = target } export function popTarget () { - Dep.target = targetStack.pop() + targetStack.pop() + Dep.target = targetStack[targetStack.length - 1] } diff --git a/test/unit/features/instance/methods-lifecycle.spec.js b/test/unit/features/instance/methods-lifecycle.spec.js index 71525be2..1e424a5c 100644 --- a/test/unit/features/instance/methods-lifecycle.spec.js +++ b/test/unit/features/instance/methods-lifecycle.spec.js @@ -1,4 +1,5 @@ import Vue from 'vue' +import Dep from 'core/observer/dep' describe('Instance methods lifecycle', () => { describe('$mount', () => { @@ -32,6 +33,26 @@ describe('Instance methods lifecycle', () => { expect(vm.$el.tagName).toBe('DIV') expect(vm.$el.textContent).toBe('hi') }) + + it('Dep.target should be undefined in lifecycle', () => { + const vm = new Vue({ + template: '
', + components: { + myComponent: { + template: '
hi
', + mounted () { + const _msg = this.msg + expect(Dep.target).toBe(undefined) + }, + computed: { + msg () { + return 1 + } + } + } + } + }).$mount() + }) }) describe('$destroy', () => {