Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,13 @@ export function stateMixin (Vue: Class<Component>) {
options.user = true
const watcher = new Watcher(vm, expOrFn, cb, options)
if (options.immediate) {
pushTarget()
try {
cb.call(vm, watcher.value)
} catch (error) {
handleError(error, vm, `callback for immediate watcher "${watcher.expression}"`)
}
popTarget()
}
return function unwatchFn () {
watcher.teardown()
Expand Down
31 changes: 31 additions & 0 deletions test/unit/features/instance/methods-lifecycle.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,37 @@ describe('Instance methods lifecycle', () => {
}
}).$mount()
})

it('Dep.target should be undefined during invocation of child immediate watcher', done => {
let calls = 0, childData
const parentUpdate = jasmine.createSpy()
new Vue({
template: '<div><my-component></my-component></div>',
updated: parentUpdate,
components: {
myComponent: {
template: '<div>{{ a }}</div>',
data() {
return childData = { a: 123 }
},
watch: {
a: {
handler() {
++calls
expect(Dep.target).toBe(undefined)
},
immediate: true
}
}
}
}
}).$mount()
expect(calls).toBe(1)
childData.a++
waitForUpdate(() => {
expect(parentUpdate.calls.count()).toBe(0)
}).then(done)
})
})

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