]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): fix multiple .once event handlers on same component (#3904)
authorLYlanfeng <23229437@qq.com>
Tue, 8 Jun 2021 13:52:45 +0000 (21:52 +0800)
committerGitHub <noreply@github.com>
Tue, 8 Jun 2021 13:52:45 +0000 (09:52 -0400)
fix #3902

packages/runtime-core/__tests__/componentEmits.spec.ts
packages/runtime-core/src/componentEmits.ts

index 35bb0fa29f14cea784ad7c94876a0b5693459053..d88981a43d4f553f0c0bc9a6d8a3986be407fc2d 100644 (file)
@@ -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', () => {
index b7fbec48992f0999cdf8c42f62b3570f5cd0f578..52900ea31761b6b25098cd2bf448ff7301e4949b 100644 (file)
@@ -149,10 +149,11 @@ export function emit(
   const onceHandler = props[handlerName + `Once`]
   if (onceHandler) {
     if (!instance.emitted) {
-      ;(instance.emitted = {} as Record<string, boolean>)[handlerName] = true
+      instance.emitted = {} as Record<any, boolean>
     } else if (instance.emitted[handlerName]) {
       return
     }
+    instance.emitted[handlerName] = true
     callWithAsyncErrorHandling(
       onceHandler,
       instance,