Vue Emits
Props carry data down from parent to child. For the opposite direction — a child telling its parent that something happened — a component emits a custom event. The parent listens with v-on / @, exactly like a DOM event.
props ↓ ↑ events
Parent ──────────▶ Child ──────────▶ Parent$emit in the template
Inside a component's template, $emit('event-name', ...args) fires an event. Declare the events a component can emit in the emits option — it documents the component and lets Vue tell custom events apart from native ones.
<div id="app">
<p>Total votes: {{ total }}</p>
<vote-button label="👍 Yes" @voted="total++"></vote-button>
<vote-button label="👎 No" @voted="total++"></vote-button>
</div>
<script type="module">
import { createApp } from 'vue';
const app = createApp({ data: () => ({ total: 0 }) });
app.component('vote-button', {
props: ['label'],
emits: ['voted'],
data: () => ({ mine: 0 }),
template: `<button @click="mine++; $emit('voted')">{{ label }} ({{ mine }})</button> `
});
app.mount('#app');
</script>Passing data with an event
Any extra arguments to $emit (or this.$emit in methods) arrive as arguments of the parent's handler.
<style>
li { margin: 4px 0; }
.done { text-decoration: line-through; color: gray; }
</style>
<div id="app">
<ul>
<li is="vue:todo-item" v-for="t in todos" :key="t.id" :todo="t"
@toggle="toggle" @remove="remove"></li>
</ul>
<p>{{ todos.filter(t => !t.done).length }} left</p>
</div>
<script type="module">
import { createApp } from 'vue';
const app = createApp({
data: () => ({
todos: [
{ id: 1, text: 'Write props lesson', done: true },
{ id: 2, text: 'Write emits lesson', done: false },
{ id: 3, text: 'Take a break', done: false }
]
}),
methods: {
toggle(id) {
const t = this.todos.find(t => t.id === id);
t.done = !t.done;
},
remove(id, text) {
this.todos = this.todos.filter(t => t.id !== id);
console.log(`removed #${id}: ${text}`);
}
}
});
app.component('todo-item', {
props: { todo: { type: Object, required: true } },
emits: ['toggle', 'remove'],
methods: {
onRemove() {
this.$emit('remove', this.todo.id, this.todo.text);
}
},
template: `
<li>
<input type="checkbox" :checked="todo.done" @change="$emit('toggle', todo.id)">
<span :class="{ done: todo.done }">{{ todo.text }}</span>
<button @click="onRemove">✕</button>
</li>`
});
app.mount('#app');
</script>The child never changes todo itself — it reports what the user wants and the parent, which owns the data, decides what to do. This is the "props down, events up" pattern.
Emitting from setup()
In the Composition API, setup receives a context object as its second argument. It contains emit:
<div id="app">
<search-box @search="onSearch"></search-box>
<p>Last search: <strong>{{ last || '—' }}</strong></p>
</div>
<script type="module">
import { createApp, ref } from 'vue';
const app = createApp({
setup() {
const last = ref('');
const onSearch = (text) => { last.value = text; };
return { last, onSearch };
}
});
app.component('search-box', {
emits: ['search'],
setup(props, { emit }) {
const text = ref('');
function submit() {
if (text.value.trim()) emit('search', text.value.trim());
text.value = '';
}
return { text, submit };
},
template: `
<form @submit.prevent="submit">
<input v-model="text" placeholder="Search…"> <button>Go</button>
</form>`
});
app.mount('#app');
</script>Validating emitted events
Like props, emits can be an object. Each key maps to null (no validation) or to a function that receives the event's arguments and returns true when they are valid:
emits: {
click: null,
submit: ({ email, password }) => {
if (email && password) return true;
console.warn('Invalid submit payload!');
return false;
}
}Naming events
| Where | Emit | Listen |
|---|---|---|
| String template / SFC | $emit('itemAdded') or $emit('item-added') |
@item-added or @itemAdded |
| In-DOM template | — | @item-added (kebab-case only) |
Vue normalises both forms, but in in-DOM templates the browser lower-cases attribute names, so @itemAdded would turn into @itemadded and never match. Using kebab-case for listeners is the safe choice everywhere.
Note: Component events do not bubble. If a grandchild emits
save, only its direct parent can listen. To pass it further up, the parent re-emits it — or you use provide/inject or a store for deeply shared state.
Tip: Always declare
emits. Undeclared listeners on a component fall through to its root element as native listeners (see Fallthrough Attributes), which can cause an event likeclickto fire twice.
Test yourself
How does a child component notify its parent that something happened?
- It changes a prop
- It emits a custom event with
$emit - It calls
parent.update() - It uses
v-modelon the parent
In setup(props, ctx), where is the emit function?
props.emitthis.$emitctx.emitwindow.emit
A grandchild emits saved. Which component can listen with @saved?
- Any ancestor
- Only its direct parent
- The root app only
- Every component on the page