📖 Scenario: You are building a simple Vue app where a child component sends a message to its parent when a button is clicked.
🎯 Goal: Create a child component that emits an event with a message. The parent component listens to this event and displays the message.
📋 What You'll Learn
Create a child component named MessageButton that emits an event called send-message with a string message.
In the parent component, listen for the send-message event from MessageButton.
Display the received message in the parent component.
Use Vue 3 Composition API with <script setup> syntax.
💡 Why This Matters
🌍 Real World
This pattern is common in Vue apps where child components need to notify parents about user actions or data changes, like form inputs or button clicks.
💼 Career
Understanding child-to-parent communication is essential for building interactive Vue applications and collaborating in teams that use component-based architecture.
Progress0 / 4 steps
1
Create the child component with a button
Create a Vue child component named MessageButton.vue with a button that says Click me. Use <script setup> and define the template with the button.
Vue
Hint
Start by creating a button inside the template. No script logic needed yet.
2
Add event emission on button click
In MessageButton.vue, import defineEmits and create an emitter named emit. Add a click event to the button that calls emit('send-message', 'Hello from child').
Vue
Hint
Use defineEmits to create an emitter and call it inside the button's click event.
3
Create the parent component and listen to the event
Create a parent component named App.vue. Import MessageButton. Add <MessageButton @send-message="handleMessage" /> in the template. Define a function handleMessage that receives the message and stores it in a reactive variable message.
Vue
Hint
Use ref to create a reactive message variable. Define handleMessage to update it. Listen to send-message event on MessageButton.
4
Complete the app with proper component registration
Ensure MessageButton is imported and used in App.vue template. The app should show the message from the child when the button is clicked.
Vue
Hint
Check that the child component is imported and used correctly. The message should update on button click.
Practice
(1/5)
1. In Vue, how does a child component send a message to its parent component?
easy
A. By modifying the parent's data directly
B. By directly calling a parent method
C. By using v-bind to pass data upwards
D. By using $emit to trigger an event
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 $emit to send a custom event to the parent, which listens for it.
Final Answer:
By using $emit to trigger an event -> Option D
Quick Check:
Child sends event with $emit = C [OK]
Hint: Child uses $emit to send events up [OK]
Common Mistakes:
Trying to modify parent data directly from child
Using v-bind to send data upwards
Calling parent methods directly from child
2. Which of the following is the correct syntax for a parent component to listen to a child's custom event named update?
easy
A. <Child @update='handleUpdate()' />
B. <Child @update='handleUpdate' />
C. <Child :update='handleUpdate' />
D. <Child v-on:update='handleUpdate()' />
Solution
Step 1: Recall Vue event listening syntax
Vue uses @eventName or v-on:eventName to 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 B
Quick Check:
Use @event='method' without () = D [OK]
Hint: Use @event='methodName' without parentheses [OK]
Common Mistakes:
Adding parentheses to method in template
Using :update which is for props, not events
Using v-on with parentheses calling method immediately
A. Event name 'update' must be declared in emits array
B. Cannot emit events from methods
C. Should use $emit instead of emit
D. Payload 123 must be a string
Solution
Step 1: Check method call syntax
The Vue instance method to emit events is $emit, not emit.
Step 2: Confirm event declaration
While declaring events in emits is recommended, not declaring does not cause this error.
Final Answer:
Should use $emit instead of emit -> Option C
Quick Check:
Use this.$emit to emit events = B [OK]
Hint: Always use this.$emit to send events [OK]
Common Mistakes:
Forgetting the $ in $emit
Thinking emits array is mandatory for emitting
Assuming payload must be string
5. You want a child component to send two different events, save and cancel, each with a payload. How should you define and emit these events properly in Vue 3 Composition API <script setup>?
hard
A. const emit = defineEmits({ save: null, cancel: null })
function onSave() { emit('save', data) }
function onCancel() { emit('cancel', reason) }
B. defineEmits(['save', 'cancel'])
function onSave() { emit('save', data) }
function onCancel() { emit('cancel', reason) }
C. defineEmits(['save', 'cancel'])
const emit = defineEmits(['save', 'cancel'])
function onSave() { emit('save', data) }
function onCancel() { emit('cancel', reason) }
D. const emit = defineEmits(['save', 'cancel'])
function onSave() { this.$emit('save', data) }
function onCancel() { this.$emit('cancel', reason) }
Solution
Step 1: Understand defineEmits usage in <script setup>
In Vue 3 Composition API with <script setup>, defineEmits returns 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) } uses defineEmits({ save: null, cancel: null }) which is valid and recommended for typing events. Then emits events with emit('save', data) and emit('cancel', reason).
Step 3: Check incorrect options
defineEmits(['save', 'cancel'])
function onSave() { emit('save', data) }
function onCancel() { emit('cancel', reason) } calls defineEmits without assigning the result to emit, so emit is undefined. defineEmits(['save', 'cancel'])
const emit = defineEmits(['save', 'cancel'])
function onSave() { emit('save', data) }
function onCancel() { emit('cancel', reason) } calls defineEmits twice which is invalid. const emit = defineEmits(['save', 'cancel'])
function onSave() { this.$emit('save', data) }
function onCancel() { this.$emit('cancel', reason) } uses this.$emit which is not available in <script setup>.
Final Answer:
const emit = defineEmits({ save: null, cancel: null }) with emit calls -> Option A
Quick Check:
Use defineEmits object + emit() in script setup = A [OK]
Hint: Use defineEmits object and emit() in script setup [OK]
Common Mistakes:
Calling defineEmits without assigning to emit
Calling defineEmits twice
Using this.$emit inside script setup
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.