Skip to content

Commit

Permalink
fix: actually disable dep collection when invoking lifecycle hooks (v…
Browse files Browse the repository at this point in the history
  • Loading branch information
ysj16 authored and hefeng committed Jan 25, 2019
1 parent 28f69a2 commit 43470a5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/core/observer/dep.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
21 changes: 21 additions & 0 deletions test/unit/features/instance/methods-lifecycle.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Vue from 'vue'
import Dep from 'core/observer/dep'

describe('Instance methods lifecycle', () => {
describe('$mount', () => {
Expand Down Expand Up @@ -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: '<div><my-component></my-component></div>',
components: {
myComponent: {
template: '<div>hi</div>',
mounted () {
const _msg = this.msg
expect(Dep.target).toBe(undefined)
},
computed: {
msg () {
return 1
}
}
}
}
}).$mount()
})
})

describe('$destroy', () => {
Expand Down

0 comments on commit 43470a5

Please sign in to comment.