0
0
Vueframework~10 mins

Emitting custom events in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Emitting custom events
Child Component Action
Emit Custom Event
Parent Listens to Event
Parent Handles Event
Update Parent State or Run Logic
The child component triggers an event, the parent listens and reacts, updating state or running code.
Execution Sample
Vue
/* Child.vue */
<script setup>
const emit = defineEmits(['notify'])
function sendNotification() {
  emit('notify', 'Hello from child')
}
</script>

<template>
  <button @click="sendNotification">Notify Parent</button>
</template>
Child component emits a 'notify' event with a message when button is clicked.
Execution Table
StepActionEvent EmittedPayloadParent ReactionParent State Change
1Child button clickednotify'Hello from child'Parent method calledparentMessage updated
2Parent updates messagenonenoneUI shows new messageparentMessage = 'Hello from child'
3No further eventsnonenoneNo actionNo change
💡 No more events emitted, parent state updated with child's message
Variable Tracker
VariableStartAfter Step 1After Step 2Final
parentMessage'''''Hello from child''Hello from child'
eventEmittednone'notify'nonenone
payloadnone'Hello from child'nonenone
Key Moments - 2 Insights
Why does the parent update only after the child emits the event?
Because the parent listens for the custom event 'notify' and only runs its handler when that event is emitted, as shown in execution_table step 1.
What happens if the child emits an event with no listener in the parent?
The event is emitted but no parent reaction occurs, so no state changes happen. This is why the parent must explicitly listen for the event.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'parentMessage' after step 1?
Aundefined
B'Hello from child'
C'' (empty string)
Dnull
💡 Hint
Check variable_tracker column 'After Step 1' for 'parentMessage'
At which step does the parent update its state with the child's message?
AStep 1
BStep 2
CStep 3
DNo update occurs
💡 Hint
See execution_table 'Parent State Change' column
If the child emitted an event named 'alert' instead of 'notify', what would happen given the current parent listener?
AParent would ignore the event
BParent would react normally
CParent would throw an error
DChild would not emit the event
💡 Hint
Parent listens only to 'notify' event as per concept_flow and code sample
Concept Snapshot
Emitting custom events in Vue:
- Child uses defineEmits(['eventName']) to declare events
- Child calls emit('eventName', payload) to send event
- Parent listens with @eventName="handler"
- Parent handler runs when event fires, updating state or logic
- Enables child-to-parent communication cleanly
Full Transcript
In Vue, child components can send messages to their parents by emitting custom events. The child declares which events it can emit using defineEmits. When an action happens in the child, like a button click, it calls emit with the event name and optional data. The parent listens for this event using the @eventName syntax in the template. When the event fires, the parent's handler runs, often updating its state or running some logic. This flow allows the child to notify the parent without directly changing the parent's data, keeping components decoupled and easy to manage.