Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): instanceWatch should pass this.proxy to source as the first argument #2753

Merged
merged 6 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
26 changes: 25 additions & 1 deletion packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
nodeOps,
serializeInner,
TestElement,
h
h,
createApp
} from '@vue/runtime-test'
import {
ITERATE_KEY,
Expand Down Expand Up @@ -857,4 +858,27 @@ 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 args: any = null
let instance: any

const source = function() {
edison1105 marked this conversation as resolved.
Show resolved Hide resolved
args = arguments
}

const Comp = defineComponent({
render() {},
created(this: any) {
instance = this
this.$watch(source, function() {})
}
})

const root = nodeOps.createElement('div')
createApp(Comp).mount(root)

expect(args.length > 0).toBe(true)
expect(instance === args[0]).toBe(true)
})
})
8 changes: 6 additions & 2 deletions packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 = () => {
Expand Down