What if your components could talk to each other without breaking anything?
Why Events for child to parent communication in Vue? - Purpose & Use Cases
Imagine you have a child component with a button, and you want to tell the parent component when the button is clicked. You try to directly change the parent's data from the child, but it feels like shouting across a noisy room without a clear way to pass the message.
Trying to change parent data directly from the child breaks the rules of Vue and causes bugs. It's like trying to control someone else's workspace without permission. This leads to messy code that is hard to fix and understand.
Vue lets the child send a clear message called an event to the parent. The parent listens for this event and reacts properly. This keeps each part focused on its own job and makes communication smooth and safe.
childComponent.data.parentValue = 'new'; // direct change, not allowed
childComponent.$emit('update', 'new'); // child sends event to parent
This lets components talk clearly and safely, making your app easier to build and maintain.
Think of a child component as a doorbell button. When pressed, it sends a signal (event) to the parent component, which is like the house owner who hears the bell and opens the door.
Directly changing parent data from child is error-prone and breaks Vue rules.
Child components send events to notify parents about actions.
Parents listen and respond, keeping code clean and communication clear.
Practice
Solution
Step 1: Understand child-to-parent communication
In Vue, child components cannot directly change parent data or call parent methods without an event.Step 2: Identify the correct Vue pattern
The child uses$emitto send a custom event to the parent, which listens for it.Final Answer:
By using $emit to trigger an event -> Option DQuick Check:
Child sends event with $emit = C [OK]
- Trying to modify parent data directly from child
- Using v-bind to send data upwards
- Calling parent methods directly from child
update?Solution
Step 1: Recall Vue event listening syntax
Vue uses@eventNameorv-on:eventNameto listen to events. The handler should be a method reference without parentheses.Step 2: Identify correct syntax
<Child @update='handleUpdate' />uses@update='handleUpdate'correctly without calling the method immediately.Final Answer:
<Child @update='handleUpdate' /> -> Option BQuick Check:
Use @event='method' without () = D [OK]
- Adding parentheses to method in template
- Using :update which is for props, not events
- Using v-on with parentheses calling method immediately
export default {
emits: ['send'],
methods: {
notify() {
this.$emit('send', 'hello')
}
}
}And parent template:
<Child @send='receiveMessage' />
What will happen when
notify() is called in the child?Solution
Step 1: Understand child emits event
The child emits 'send' event with payload 'hello' when notify() is called.Step 2: Parent listens and handles event
The parent listens to 'send' event with @send='receiveMessage', so it calls receiveMessage('hello').Final Answer:
Parent's receiveMessage method is called with argument 'hello' -> Option AQuick Check:
Child $emit triggers parent handler with payload = A [OK]
- Thinking emits array is required to receive event
- Assuming parent won't get event without extra code
- Confusing event name or payload usage
export default {
methods: {
sendData() {
this.emit('update', 123)
}
}
}Solution
Step 1: Check method call syntax
The Vue instance method to emit events is$emit, notemit.Step 2: Confirm event declaration
While declaring events inemitsis recommended, not declaring does not cause this error.Final Answer:
Should use $emit instead of emit -> Option CQuick Check:
Use this.$emit to emit events = B [OK]
- Forgetting the $ in $emit
- Thinking emits array is mandatory for emitting
- Assuming payload must be string
save and cancel, each with a payload. How should you define and emit these events properly in Vue 3 Composition API <script setup>?Solution
Step 1: Understand defineEmits usage in <script setup>
In Vue 3 Composition API with <script setup>,defineEmitsreturns an emit function. It can accept an object to define event names and validators or an array of event names.Step 2: Identify correct syntax for multiple events
const emit = defineEmits({ save: null, cancel: null }) function onSave() { emit('save', data) } function onCancel() { emit('cancel', reason) }usesdefineEmits({ save: null, cancel: null })which is valid and recommended for typing events. Then emits events withemit('save', data)andemit('cancel', reason).Step 3: Check incorrect options
defineEmits(['save', 'cancel']) function onSave() { emit('save', data) } function onCancel() { emit('cancel', reason) }callsdefineEmitswithout assigning the result toemit, soemitis undefined.defineEmits(['save', 'cancel']) const emit = defineEmits(['save', 'cancel']) function onSave() { emit('save', data) } function onCancel() { emit('cancel', reason) }callsdefineEmitstwice which is invalid.const emit = defineEmits(['save', 'cancel']) function onSave() { this.$emit('save', data) } function onCancel() { this.$emit('cancel', reason) }usesthis.$emitwhich is not available in<script setup>.Final Answer:
const emit = defineEmits({ save: null, cancel: null }) with emit calls -> Option AQuick Check:
Use defineEmits object + emit() in script setup = A [OK]
- Calling defineEmits without assigning to emit
- Calling defineEmits twice
- Using this.$emit inside script setup
Ready to prove it?
Pass the test and this pattern moves unknown
