mirror of
https://github.com/vuejs/vue.git
synced 2024-11-21 12:18:54 +00:00
fix(watch): new property addition should trigger deep watcher with getter
close #12967 close #12972
This commit is contained in:
parent
a174c29dab
commit
6d857f5bee
@ -185,8 +185,11 @@ function doWatch(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const instance = currentInstance
|
const instance = currentInstance
|
||||||
const call = (fn: Function, type: string, args: any[] | null = null) =>
|
const call = (fn: Function, type: string, args: any[] | null = null) => {
|
||||||
invokeWithErrorHandling(fn, null, args, instance, type)
|
const res = invokeWithErrorHandling(fn, null, args, instance, type)
|
||||||
|
if (deep && res && res.__ob__) res.__ob__.dep.depend()
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
let getter: () => any
|
let getter: () => any
|
||||||
let forceTrigger = false
|
let forceTrigger = false
|
||||||
@ -209,6 +212,7 @@ function doWatch(
|
|||||||
if (isRef(s)) {
|
if (isRef(s)) {
|
||||||
return s.value
|
return s.value
|
||||||
} else if (isReactive(s)) {
|
} else if (isReactive(s)) {
|
||||||
|
s.__ob__.dep.depend()
|
||||||
return traverse(s)
|
return traverse(s)
|
||||||
} else if (isFunction(s)) {
|
} else if (isFunction(s)) {
|
||||||
return call(s, WATCHER_GETTER)
|
return call(s, WATCHER_GETTER)
|
||||||
|
@ -1200,4 +1200,35 @@ describe('api: watch', () => {
|
|||||||
expect(parentSpy).toHaveBeenCalledTimes(1)
|
expect(parentSpy).toHaveBeenCalledTimes(1)
|
||||||
expect(childSpy).toHaveBeenCalledTimes(1)
|
expect(childSpy).toHaveBeenCalledTimes(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// #12967
|
||||||
|
test('trigger when adding new property with Vue.set (getter)', async () => {
|
||||||
|
const spy = vi.fn()
|
||||||
|
const r = reactive({ exist: 5 })
|
||||||
|
watch(() => r, spy, { deep: true })
|
||||||
|
set(r, 'add', 1)
|
||||||
|
|
||||||
|
await nextTick()
|
||||||
|
expect(spy).toHaveBeenCalledTimes(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('trigger when adding new property with Vue.set (getter in array source)', async () => {
|
||||||
|
const spy = vi.fn()
|
||||||
|
const r = reactive({ exist: 5 })
|
||||||
|
watch([() => r], spy, { deep: true })
|
||||||
|
set(r, 'add', 1)
|
||||||
|
|
||||||
|
await nextTick()
|
||||||
|
expect(spy).toHaveBeenCalledTimes(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('trigger when adding new property with Vue.set (reactive in array source)', async () => {
|
||||||
|
const spy = vi.fn()
|
||||||
|
const r = reactive({ exist: 5 })
|
||||||
|
watch([r], spy, { deep: true })
|
||||||
|
set(r, 'add', 1)
|
||||||
|
|
||||||
|
await nextTick()
|
||||||
|
expect(spy).toHaveBeenCalledTimes(1)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user