Menu
CoddyTech

Emitting Events

Part of the Fundamentals section of Coddy's Vue journey. Lesson 36 of 55.

A child can request an action without owning the parent's state. This button emits clap; the parent listener increments its own applause ref.

// Child script setup:
const emit = defineEmits(['clap']);
// Child template: <button @click="emit('clap')">Clap</button>
// Parent template: <ClapButton @clap="applause++" />

Declare custom events with defineEmits, a compiler macro that needs no import. The event name must match the parent listener. Component events do not automatically bubble through every ancestor; a direct parent listens to the component it renders.

challenge icon

Challenge

Medium

Create JoinButton.vue that declares and emits a join event when its button is clicked. The parent starts with one member, listens for the event, and shows Members: 1 initially in #result. Put the child inside #join. Each event adds one member in the parent.

Try it yourself

<!doctype html><html><head><meta charset="UTF-8"><title>Vue practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals