Consider this Vue 3 component using the Composition API. What will be the output in the parent component when the button is clicked?
<script setup> import { defineEmits } from 'vue' const emit = defineEmits(['update']) function handleClick() { emit('update', 'Hello from child') } </script> <template> <button @click="handleClick">Click me</button> </template>
Think about how Vue components communicate upward using events.
Using defineEmits in Vue 3 Composition API allows the child to emit custom events. The parent can listen to these events and receive the payload.
Choose the correct syntax to emit a custom event named 'save' with two arguments: id and data.
Vue emits can send multiple arguments separately.
The emit function accepts the event name followed by any number of arguments. These arguments are passed to the event listener in order.
Look at the code below. The parent never receives the 'submit' event. What is the cause?
<script setup> const emit = defineEmits(['submit']) function onSubmit() { emit('submit') } </script> <template> <button @click="onSubmit">Submit</button> </template>
Check if all Vue functions are properly imported.
In Vue 3 script setup, you must import defineEmits from 'vue' to use it. Without import, defineEmits is undefined, so emit is not set.
A child component emits a 'change' event with a payload object { count: 5 }. The parent listens and updates its total state by adding count. What is the value of total after the event?
<template> <Child @change="handleChange" /> <p>Total: {{ total }}</p> </template> <script setup> import { ref } from 'vue' const total = ref(0) function handleChange(payload) { total.value += payload.count } </script>
Think about how the reactive ref updates with the event payload.
The parent starts with total = 0. When the child emits { count: 5 }, the handler adds 5 to total.value. So the new total is 5.
Which reason best explains why emitting custom events is preferred over directly modifying parent props in Vue components?
Think about Vue's design principle for data flow between components.
Vue enforces one-way data flow: parents pass props down, children emit events up. Props are read-only to children to avoid unexpected side effects and keep components independent.