Challenge - 5 Problems
Vue Event Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when the child emits an event?
Consider a Vue 3 component where a child emits an event named
update. What will the parent component do when it listens to this event?Vue
<template> <ChildComponent @update="handleUpdate" /> </template> <script setup> function handleUpdate(payload) { console.log('Received:', payload) } </script>
Attempts:
2 left
💡 Hint
Think about how Vue handles custom events emitted by children.
✗ Incorrect
When a child emits an event, the parent listens using @eventName and runs the assigned method with the payload.
📝 Syntax
intermediate2:00remaining
Identify the correct syntax to emit an event from a child component
Which of the following is the correct way to emit an event named
save with a payload { id: 1 } inside a Vue 3 child component using defineEmits?Attempts:
2 left
💡 Hint
In Vue 3
<script setup>, defineEmits returns a function to call.✗ Incorrect
defineEmits returns an emit function you call with the event name and payload.
🔧 Debug
advanced2:00remaining
Why does the parent not receive the event?
A child component emits an event
change, but the parent does not respond. What is the most likely cause?Vue
<!-- Parent.vue --> <template> <ChildComponent @changed="onChange" /> </template> <script setup> function onChange() { console.log('Changed event received') } </script> <!-- Child.vue --> <script setup> const emit = defineEmits(['change']) function trigger() { emit('change') } </script>
Attempts:
2 left
💡 Hint
Check the event names in the parent and child components.
✗ Incorrect
Event names are case-sensitive and must match exactly between emit and listener.
❓ state_output
advanced2:00remaining
What is the parent's data after child emits an event?
Given the following parent and child components, what will be the value of
message in the parent after the child emits the event?Vue
<!-- Parent.vue --> <template> <ChildComponent @send-message="updateMessage" /> <p>{{ message }}</p> </template> <script setup> import { ref } from 'vue' const message = ref('') function updateMessage(text) { message.value = text } </script> <!-- Child.vue --> <script setup> const emit = defineEmits(['send-message']) emit('send-message', 'Hello from child') </script>
Attempts:
2 left
💡 Hint
The emit is called top-level during the child's setup phase.
✗ Incorrect
Calling emit at the top level of the child's <script setup> executes during component setup, synchronously invoking the parent's updateMessage function and setting message to 'Hello from child'.
🧠 Conceptual
expert2:00remaining
Why use events for child to parent communication instead of props?
In Vue, why is emitting events from child to parent preferred over trying to modify parent props directly?
Attempts:
2 left
💡 Hint
Think about Vue's data flow rules and how props are designed.
✗ Incorrect
Vue enforces one-way data flow: props go from parent to child and are read-only. Children must emit events to request changes.