Skip to content

Commit b5f91ff

Browse files
authored
fix(watch): allow handler to be a string (#1775)
fix #1774
1 parent b0d01e9 commit b5f91ff

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

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

+20-3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ describe('api: options', () => {
112112
const spyA = jest.fn(returnThis)
113113
const spyB = jest.fn(returnThis)
114114
const spyC = jest.fn(returnThis)
115+
const spyD = jest.fn(returnThis)
115116

116117
let ctx: any
117118
const Comp = {
@@ -121,7 +122,8 @@ describe('api: options', () => {
121122
bar: 2,
122123
baz: {
123124
qux: 3
124-
}
125+
},
126+
qux: 4
125127
}
126128
},
127129
watch: {
@@ -132,10 +134,14 @@ describe('api: options', () => {
132134
baz: {
133135
handler: spyC,
134136
deep: true
137+
},
138+
qux: {
139+
handler: 'onQuxChange'
135140
}
136141
},
137142
methods: {
138-
onFooChange: spyA
143+
onFooChange: spyA,
144+
onQuxChange: spyD
139145
},
140146
render() {
141147
ctx = this
@@ -164,6 +170,11 @@ describe('api: options', () => {
164170
expect(spyC).toHaveBeenCalledTimes(1)
165171
// new and old objects have same identity
166172
assertCall(spyC, 0, [{ qux: 4 }, { qux: 4 }])
173+
174+
ctx.qux++
175+
await nextTick()
176+
expect(spyD).toHaveBeenCalledTimes(1)
177+
assertCall(spyD, 0, [5, 4])
167178
})
168179

169180
test('watch array', async () => {
@@ -707,7 +718,10 @@ describe('api: options', () => {
707718
test('Expected a function as watch handler', () => {
708719
const Comp = {
709720
watch: {
710-
foo: 'notExistingMethod'
721+
foo: 'notExistingMethod',
722+
foo2: {
723+
handler: 'notExistingMethod2'
724+
}
711725
},
712726
render() {}
713727
}
@@ -718,6 +732,9 @@ describe('api: options', () => {
718732
expect(
719733
'Invalid watch handler specified by key "notExistingMethod"'
720734
).toHaveBeenWarned()
735+
expect(
736+
'Invalid watch handler specified by key "notExistingMethod2"'
737+
).toHaveBeenWarned()
721738
})
722739

723740
test('Invalid watch option', () => {

packages/runtime-core/src/componentOptions.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ export type ExtractComputedReturns<T extends any> = {
266266
type WatchOptionItem =
267267
| string
268268
| WatchCallback
269-
| { handler: WatchCallback } & WatchOptions
269+
| { handler: WatchCallback | string } & WatchOptions
270270

271271
type ComponentWatchOptionItem = WatchOptionItem | WatchOptionItem[]
272272

@@ -704,7 +704,14 @@ function createWatcher(
704704
if (isArray(raw)) {
705705
raw.forEach(r => createWatcher(r, ctx, publicThis, key))
706706
} else {
707-
watch(getter, raw.handler.bind(publicThis), raw)
707+
const handler = isFunction(raw.handler)
708+
? raw.handler.bind(publicThis)
709+
: (ctx[raw.handler] as WatchCallback)
710+
if (isFunction(handler)) {
711+
watch(getter, handler, raw)
712+
} else if (__DEV__) {
713+
warn(`Invalid watch handler specified by key "${raw.handler}"`, handler)
714+
}
708715
}
709716
} else if (__DEV__) {
710717
warn(`Invalid watch option: "${key}"`)

0 commit comments

Comments
 (0)