0
0
Vueframework~10 mins

Events for child to parent communication in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Events for child to parent communication
Child Component
Parent Component
Parent updates state or reacts
The child sends an event with data upward, and the parent listens and reacts to it.
Execution Sample
Vue
<template>
  <Child @notify="handleNotify" />
</template>
<script setup>
function handleNotify(message) {
  alert(message)
}
</script>
Parent listens to child's 'notify' event and shows an alert with the message.
Execution Table
StepActionComponentEvent EmittedParent ListenerParent Reaction
1Child rendersChildNoneNoneNone
2User clicks button in ChildChildEmits 'notify' with 'Hello!'Listens for 'notify'Calls handleNotify('Hello!')
3Parent handles eventParentNoneReceived 'notify'Shows alert with 'Hello!'
4Event cycle endsBothNoneNoneNo further action
💡 Event emitted by child and handled by parent, communication complete.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
messageundefined'Hello!''Hello!''Hello!'
parentStateinitialinitialupdated if anyupdated if any
Key Moments - 2 Insights
Why does the parent need to listen for the child's event?
Because the child emits an event, but the parent only reacts if it listens for that event, as shown in execution_table step 2 and 3.
Can the child directly change the parent's data?
No, the child cannot directly change parent's data; it must emit an event that the parent listens to and then updates its own data, as seen in the flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what event does the child emit at step 2?
A'update' with no data
B'notify' with message 'Hello!'
C'click' event
DNo event emitted
💡 Hint
Check the 'Event Emitted' column at step 2 in the execution_table.
At which step does the parent call the handler function?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Parent Reaction' column in the execution_table.
If the parent does not listen for the child's event, what happens?
AEvent is emitted but parent ignores it
BChild cannot emit events
CParent still reacts automatically
DChild updates parent data directly
💡 Hint
Refer to the concept_flow and key_moments about event listening.
Concept Snapshot
Child emits event with data using $emit.
Parent listens with @eventName in template.
Parent handler receives data and updates state.
This enables child-to-parent communication.
Child cannot directly change parent data.
Events flow upward only.
Full Transcript
In Vue, child components communicate with parents by emitting events. The child uses $emit to send an event with optional data. The parent listens for this event using the @eventName syntax in its template. When the event occurs, the parent runs a handler function that can update its state or perform actions. This pattern keeps components decoupled and allows data to flow upward safely. The child cannot directly modify the parent's data; it must notify the parent through events. This example shows a child emitting a 'notify' event with a message, and the parent showing an alert when it receives it.