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
MediumCreate 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>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
2Reactive State
Reactive RefsUpdating Refs in ScriptEvent Handler ArgumentsReactive ObjectsRecap: Reactive State5Conditions and Lists
Conditional BranchesShow without UnmountingRendering ListsStable List IdentityRecap: Conditions8Component Communication
Emitting EventsEvent PayloadsLifting Shared StateDefault and Named SlotsRecap: Shared Panel11Project: Community Board
Board StructureAdding Community Tasks