Skip to content

Commit ccbb02a

Browse files
ysj16aJean
authored andcommitted
fix: actually disable dep collection when invoking lifecycle hooks (vuejs#9095)
fix vuejs#9046
1 parent 52d499c commit ccbb02a

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/core/observer/dep.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ export default class Dep {
5555
Dep.target = null
5656
const targetStack = []
5757

58-
export function pushTarget (_target: ?Watcher) {
59-
if (Dep.target) targetStack.push(Dep.target)
60-
Dep.target = _target
58+
export function pushTarget (target: ?Watcher) {
59+
targetStack.push(target)
60+
Dep.target = target
6161
}
6262

6363
export function popTarget () {
64-
Dep.target = targetStack.pop()
64+
targetStack.pop()
65+
Dep.target = targetStack[targetStack.length - 1]
6566
}

test/unit/features/instance/methods-lifecycle.spec.js

+21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vue from 'vue'
2+
import Dep from 'core/observer/dep'
23

34
describe('Instance methods lifecycle', () => {
45
describe('$mount', () => {
@@ -32,6 +33,26 @@ describe('Instance methods lifecycle', () => {
3233
expect(vm.$el.tagName).toBe('DIV')
3334
expect(vm.$el.textContent).toBe('hi')
3435
})
36+
37+
it('Dep.target should be undefined in lifecycle', () => {
38+
const vm = new Vue({
39+
template: '<div><my-component></my-component></div>',
40+
components: {
41+
myComponent: {
42+
template: '<div>hi</div>',
43+
mounted () {
44+
const _msg = this.msg
45+
expect(Dep.target).toBe(undefined)
46+
},
47+
computed: {
48+
msg () {
49+
return 1
50+
}
51+
}
52+
}
53+
}
54+
}).$mount()
55+
})
3556
})
3657

3758
describe('$destroy', () => {

0 commit comments

Comments
 (0)