Skip to content

Commit 71c9536

Browse files
authoredApr 14, 2022
fix(runtime-core): ensure custom events are not emitted anymore after unmount. (#5679)
close #5674
1 parent 8e29ef6 commit 71c9536

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed
 

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

+27-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
defineComponent,
77
h,
88
nodeOps,
9-
toHandlers
9+
toHandlers,
10+
nextTick
1011
} from '@vue/runtime-test'
1112
import { isEmitListener } from '../src/componentEmits'
1213

@@ -374,4 +375,29 @@ describe('component: emit', () => {
374375
// PascalCase option
375376
expect(isEmitListener(options, 'onFooBaz')).toBe(true)
376377
})
378+
379+
test('does not emit after unmount', async () => {
380+
const fn = jest.fn()
381+
const Foo = defineComponent({
382+
emits: ['closing'],
383+
async beforeUnmount() {
384+
await this.$nextTick()
385+
this.$emit('closing', true)
386+
},
387+
render() {
388+
return h('div')
389+
}
390+
})
391+
const Comp = () =>
392+
h(Foo, {
393+
onClosing: fn
394+
})
395+
396+
const el = nodeOps.createElement('div')
397+
render(h(Comp), el)
398+
await nextTick()
399+
render(null, el)
400+
await nextTick()
401+
expect(fn).not.toHaveBeenCalled()
402+
})
377403
})

‎packages/runtime-core/src/componentEmits.ts

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export function emit(
7373
event: string,
7474
...rawArgs: any[]
7575
) {
76+
if (instance.isUnmounted) return
7677
const props = instance.vnode.props || EMPTY_OBJ
7778

7879
if (__DEV__) {

0 commit comments

Comments
 (0)
Please sign in to comment.