From ca327e035eccc1f4ef100bc0cf5173c5ec18897e Mon Sep 17 00:00:00 2001 From: Thorsten Luenborg Date: Thu, 7 Apr 2022 08:32:24 +0200 Subject: [PATCH] fix(runtime-core): ensure custom events are not emitted anymore after unmount. --- .../__tests__/componentEmits.spec.ts | 28 ++++++++++++++++++- packages/runtime-core/src/componentEmits.ts | 1 + 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/componentEmits.spec.ts b/packages/runtime-core/__tests__/componentEmits.spec.ts index d88981a43d4..8963bda2649 100644 --- a/packages/runtime-core/__tests__/componentEmits.spec.ts +++ b/packages/runtime-core/__tests__/componentEmits.spec.ts @@ -6,7 +6,8 @@ import { defineComponent, h, nodeOps, - toHandlers + toHandlers, + nextTick } from '@vue/runtime-test' import { isEmitListener } from '../src/componentEmits' @@ -374,4 +375,29 @@ describe('component: emit', () => { // PascalCase option expect(isEmitListener(options, 'onFooBaz')).toBe(true) }) + + test('does not emit after unmount', async () => { + const fn = jest.fn() + const Foo = defineComponent({ + emits: ['closing'], + async beforeUnmount() { + await this.$nextTick() + this.$emit('closing', true) + }, + render() { + return h('div') + } + }) + const Comp = () => + h(Foo, { + onClosing: fn + }) + + const el = nodeOps.createElement('div') + render(h(Comp), el) + await nextTick() + render(null, el) + await nextTick() + expect(fn).not.toHaveBeenCalled() + }) }) diff --git a/packages/runtime-core/src/componentEmits.ts b/packages/runtime-core/src/componentEmits.ts index 4d028913d15..3983e5885b3 100644 --- a/packages/runtime-core/src/componentEmits.ts +++ b/packages/runtime-core/src/componentEmits.ts @@ -73,6 +73,7 @@ export function emit( event: string, ...rawArgs: any[] ) { + if (instance.isUnmounted) return const props = instance.vnode.props || EMPTY_OBJ if (__DEV__) {