]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): instanceWatch should pass `this.proxy` to source as the first...
authoredison <daiwei521@126.com>
Tue, 9 Feb 2021 07:00:32 +0000 (15:00 +0800)
committerGitHub <noreply@github.com>
Tue, 9 Feb 2021 07:00:32 +0000 (08:00 +0100)
packages/runtime-core/__tests__/apiWatch.spec.ts
packages/runtime-core/src/apiWatch.ts

index 1836b6e93922beb0dc9af963dd0e679e2b298350..30248072a9b2cb5d522aa507c631b9845c0c9be2 100644 (file)
@@ -15,7 +15,8 @@ import {
   nodeOps,
   serializeInner,
   TestElement,
-  h
+  h,
+  createApp
 } from '@vue/runtime-test'
 import {
   ITERATE_KEY,
@@ -857,4 +858,23 @@ describe('api: watch', () => {
 
     expect(instance!.effects![0].active).toBe(false)
   })
+
+  test('this.$watch should pass `this.proxy` to watch source as the first argument ', () => {
+    let instance: any
+    const source = jest.fn()
+
+    const Comp = defineComponent({
+      render() {},
+      created(this: any) {
+        instance = this
+        this.$watch(source, function() {})
+      }
+    })
+
+    const root = nodeOps.createElement('div')
+    createApp(Comp).mount(root)
+
+    expect(instance).toBeDefined()
+    expect(source).toHaveBeenCalledWith(instance)
+  })
 })
index 55bbbb8f9a783aba45d07659108c889c96a8f2af..0e0a549c8a9082e156c8b05ed9f04dfdc596f449 100644 (file)
@@ -181,7 +181,9 @@ function doWatch(
         } else if (isReactive(s)) {
           return traverse(s)
         } else if (isFunction(s)) {
-          return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER)
+          return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER, [
+            instance && (instance.proxy as any)
+          ])
         } else {
           __DEV__ && warnInvalidSource(s)
         }
@@ -190,7 +192,9 @@ function doWatch(
     if (cb) {
       // getter with cb
       getter = () =>
-        callWithErrorHandling(source, instance, ErrorCodes.WATCH_GETTER)
+        callWithErrorHandling(source, instance, ErrorCodes.WATCH_GETTER, [
+          instance && (instance.proxy as any)
+        ])
     } else {
       // no cb -> simple effect
       getter = () => {