Explain it simply
Component Events, in plain language
The parent sends data down through props, and the child sends actions back up through events. defineEmits documents which events a component can emit and returns an emit function. Stick to kebab-case for event names.
Problem → Why → Solution
The problem — why this concept matters
Assuming an event will automatically bubble up multiple levels of the component tree
A common mistake
Spelling the event name differently in the parent and the child
Core concept — understand how it works
Use defineEmits and custom events to send actions from a child component up to its parent.
- props or event
- component and template
- rendered Vue UI
Declare a custom event
Core syntax or pattern
<!-- QuantityPicker.vue -->
<script setup>
const props = defineProps({ modelValue: Number })
const emit = defineEmits(['update:modelValue'])
</script>
<template>
<button @click="emit('update:modelValue', props.modelValue - 1)">−</button>
<span>{{ props.modelValue }}</span>
<button @click="emit('update:modelValue', props.modelValue + 1)">+</button>
</template>Use defineEmits and custom events to send actions from a child component up to its parent.
Read the code step by step
- props or event
<!-- QuantityPicker.vue -->The parent sends data down through props, and the child sends actions back up through events. defineEmits documents which events a component can emit and returns an emit function. Stick to kebab-case for event names.
- component and template
<template>Use defineEmits and custom events to send actions from a child component up to its parent.
- rendered Vue UI
</template>Apply Component Events in a small example and test normal, edge, and failure cases.
- props or event
- component and template
- rendered Vue UI
Success
Declare a custom event
Failure
Assuming an event will automatically bubble up multiple levels of the component tree
Let's break it down simply
The parent sends data down through props, and the child sends actions back up through events. defineEmits documents which events a component can emit and returns an emit function. Stick to kebab-case for event names.
<!-- QuantityPicker.vue -->
<script setup>
const props = defineProps({ modelValue: Number })
const emit = defineEmits(['update:modelValue'])
</script>
<template>
<button @click="emit('update:modelValue', props.modelValue - 1)">−</button>
<span>{{ props.modelValue }}</span>
<button @click="emit('update:modelValue', props.modelValue + 1)">+</button>
</template>− 2 +Try it yourself
Build a TaskItem component with a delete button, and send the task id to the parent as the delete event's payload.
Component Events — Vue.js
Real-world usage
How to apply it in a real project
Apply Component Events in a small example and test normal, edge, and failure cases.
Mini challenge
Build it yourself
Build a TaskItem component with a delete button, and send the task id to the parent as the delete event's payload.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
<!-- QuantityPicker.vue -->
<script setup>
const props = defineProps({ modelValue: Number })
const emit = defineEmits(['update:modelValue'])
</script>
<template>
<button @click="emit('update:modelValue', props.modelValue - 1)">−</button>
<span>{{ props.modelValue }}</span>
<button @click="emit('update:modelValue', props.modelValue + 1)">+</button>
</template>Expected: Declare a custom event
Show hint
Apply Component Events in a small example and test normal, edge, and failure cases.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- props or event → component and template → rendered Vue UI
- Use defineEmits and custom events to send actions from a child component up to its parent.
- Assuming an event will automatically bubble up multiple levels of the component tree
- Apply Component Events in a small example and test normal, edge, and failure cases.
Component Events — Vue.js