Thuta Learning
Vue.js
IntermediateWeb DevelopmentBeginner

Component Events

Vue.jsLesson 1220 min

What you'll walk away with

  • Declare a custom event
  • Send a payload
  • Wire up a listener on the parent

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.

Mental model
  1. props or event
  2. component and template
  3. rendered Vue UI

Declare a custom event

Core syntax or pattern

vue
<!-- 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

  1. 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.

  2. component and template<template>

    Use defineEmits and custom events to send actions from a child component up to its parent.

  3. rendered Vue UI</template>

    Apply Component Events in a small example and test normal, edge, and failure cases.

Execution flow
  1. props or event
  2. component and template
  3. 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.

vue
<!-- 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>
You should see
− 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
vue
<!-- 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

What best describes the core idea of Component Events?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

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

Easy traps

  • Assuming an event will automatically bubble up multiple levels of the component tree
  • Spelling the event name differently in the parent and the child

Exercise

Build a TaskItem component with a delete button, and send the task id to the parent as the delete event's payload.

You'll know it worked when: − 2 +

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Component Events | Thuta Learning