Performance: Emitting custom events
This affects interaction responsiveness and event handling efficiency in Vue components.
<template> <button @click="$emit('update', data.value)">Click</button> </template> <script setup> const data = { value: 1 }; </script>
<template> <button @click="$emit('update', data)">Click</button> </template> <script setup> const data = { value: 1 }; </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Emit large objects as event payload | Multiple reactive updates | Multiple reflows if DOM changes | High paint cost due to layout thrashing | [X] Bad |
| Emit simple values as event payload | Minimal reactive updates | Single reflow if needed | Low paint cost | [OK] Good |
$emit method do in a Vue child component?$emit$emit method is used inside a child component to send a message or event to its parent.$emit$emit sends events to parent [OK]update with a payload 42 inside a Vue component?this.$emit(eventName, payload).this.$emit('update', 42) correctly uses this.$emit('update', 42), which is the proper method call.this.$emit to emit events [OK]export default {
methods: {
sendAlert() {
this.$emit('alert', 'Hello!')
}
}
}<ChildComponent @alert="showMessage" />
sendAlert() is called?showMessage.sendAlert() runs, the parent receives the event and calls showMessage('Hello!').showMessage is called with 'Hello!' as argument. -> Option Amethods: {
notify() {
$emit('notify')
}
}$emit must be called with this context inside methods.this$emit('notify') without this, causing a ReferenceError.this before $emit causes a ReferenceError. -> Option Bthis.$emit inside methods [OK]change only when a new value is different from the old one. Which code correctly implements this logic inside the child?this.value.if (newVal !== this.value) { this.$emit('change', newVal) } correctly emits only on change. Code that emits unconditionally sends every time. Code using if (newVal == this.value) emits when values match (wrong logic). Code with this.emit uses invalid syntax.Pass the test and this pattern moves unknown