From 011dee8644bb52f5bdc6365c6e8404936d57e2cd Mon Sep 17 00:00:00 2001 From: LYlanfeng <23229437@qq.com> Date: Tue, 8 Jun 2021 21:52:45 +0800 Subject: [PATCH] fix(runtime-core): fix multiple .once event handlers on same component (#3904) fix #3902 --- packages/runtime-core/__tests__/componentEmits.spec.ts | 10 ++++++++-- packages/runtime-core/src/componentEmits.ts | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/__tests__/componentEmits.spec.ts b/packages/runtime-core/__tests__/componentEmits.spec.ts index 35bb0fa2..d88981a4 100644 --- a/packages/runtime-core/__tests__/componentEmits.spec.ts +++ b/packages/runtime-core/__tests__/componentEmits.spec.ts @@ -245,21 +245,27 @@ describe('component: emit', () => { const Foo = defineComponent({ render() {}, emits: { - foo: null + foo: null, + bar: null }, created() { this.$emit('foo') this.$emit('foo') + this.$emit('bar') + this.$emit('bar') } }) const fn = jest.fn() + const barFn = jest.fn() render( h(Foo, { - onFooOnce: fn + onFooOnce: fn, + onBarOnce: barFn }), nodeOps.createElement('div') ) expect(fn).toHaveBeenCalledTimes(1) + expect(barFn).toHaveBeenCalledTimes(1) }) test('.once with normal listener of the same name', () => { diff --git a/packages/runtime-core/src/componentEmits.ts b/packages/runtime-core/src/componentEmits.ts index b7fbec48..52900ea3 100644 --- a/packages/runtime-core/src/componentEmits.ts +++ b/packages/runtime-core/src/componentEmits.ts @@ -149,10 +149,11 @@ export function emit( const onceHandler = props[handlerName + `Once`] if (onceHandler) { if (!instance.emitted) { - ;(instance.emitted = {} as Record)[handlerName] = true + instance.emitted = {} as Record } else if (instance.emitted[handlerName]) { return } + instance.emitted[handlerName] = true callWithAsyncErrorHandling( onceHandler, instance,