fix(watch): allow handler to be a string (#1775)

fix #1774
This commit is contained in:
Eduardo San Martin Morote
2020-08-04 18:42:47 +02:00
committed by GitHub
parent b0d01e9db9
commit b5f91ff570
2 changed files with 29 additions and 5 deletions

View File

@@ -266,7 +266,7 @@ export type ExtractComputedReturns<T extends any> = {
type WatchOptionItem =
| string
| WatchCallback
| { handler: WatchCallback } & WatchOptions
| { handler: WatchCallback | string } & WatchOptions
type ComponentWatchOptionItem = WatchOptionItem | WatchOptionItem[]
@@ -704,7 +704,14 @@ function createWatcher(
if (isArray(raw)) {
raw.forEach(r => createWatcher(r, ctx, publicThis, key))
} else {
watch(getter, raw.handler.bind(publicThis), raw)
const handler = isFunction(raw.handler)
? raw.handler.bind(publicThis)
: (ctx[raw.handler] as WatchCallback)
if (isFunction(handler)) {
watch(getter, handler, raw)
} else if (__DEV__) {
warn(`Invalid watch handler specified by key "${raw.handler}"`, handler)
}
}
} else if (__DEV__) {
warn(`Invalid watch option: "${key}"`)