Skip to content

Commit ec8fd10

Browse files
authored
fix(runtime-core): instanceWatch should pass this.proxy to source as the first argument (#2753)
1 parent bd1240c commit ec8fd10

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

packages/runtime-core/__tests__/apiWatch.spec.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
nodeOps,
1616
serializeInner,
1717
TestElement,
18-
h
18+
h,
19+
createApp
1920
} from '@vue/runtime-test'
2021
import {
2122
ITERATE_KEY,
@@ -857,4 +858,23 @@ describe('api: watch', () => {
857858

858859
expect(instance!.effects![0].active).toBe(false)
859860
})
861+
862+
test('this.$watch should pass `this.proxy` to watch source as the first argument ', () => {
863+
let instance: any
864+
const source = jest.fn()
865+
866+
const Comp = defineComponent({
867+
render() {},
868+
created(this: any) {
869+
instance = this
870+
this.$watch(source, function() {})
871+
}
872+
})
873+
874+
const root = nodeOps.createElement('div')
875+
createApp(Comp).mount(root)
876+
877+
expect(instance).toBeDefined()
878+
expect(source).toHaveBeenCalledWith(instance)
879+
})
860880
})

packages/runtime-core/src/apiWatch.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ function doWatch(
181181
} else if (isReactive(s)) {
182182
return traverse(s)
183183
} else if (isFunction(s)) {
184-
return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER)
184+
return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER, [
185+
instance && (instance.proxy as any)
186+
])
185187
} else {
186188
__DEV__ && warnInvalidSource(s)
187189
}
@@ -190,7 +192,9 @@ function doWatch(
190192
if (cb) {
191193
// getter with cb
192194
getter = () =>
193-
callWithErrorHandling(source, instance, ErrorCodes.WATCH_GETTER)
195+
callWithErrorHandling(source, instance, ErrorCodes.WATCH_GETTER, [
196+
instance && (instance.proxy as any)
197+
])
194198
} else {
195199
// no cb -> simple effect
196200
getter = () => {

0 commit comments

Comments
 (0)