Skip to content

Component Events

This page assumes you’ve already read the Components Basics. Read that first if you are new to components.

A component can emit custom events directly in template expressions (e.g. in a v-on handler) using the built-in $emit method:

<!-- MyComponent -->
<button @click="$emit('someEvent')">Click Me</button>

The $emit() method is also available on the component instance as this.$emit():

export default {
methods: {
submit() {
this.$emit('someEvent')
}
}
}

The parent can then listen to it using v-on:

<MyComponent @some-event="callback" />

The .once modifier is also supported on component event listeners:

<MyComponent @some-event.once="callback" />

Like components and props, event names provide an automatic case transformation. Notice we emitted a camelCase event, but can listen for it using a kebab-cased listener in the parent. As with props casing, we recommend using kebab-cased event listeners in templates.

It’s sometimes useful to emit a specific value with an event. For example, we might want the <BlogPost> component to determine how much to enlarge the text. In such cases, we can pass additional arguments to $emit to provide this value.

<button @click="$emit('increaseBy', 1)">
Increase by 1
</button>

Then, when we listen to the event in the parent, we can use an inline arrow function as the listener, which allows us to access the event argument:

<MyButton @increase-by="(n) => count += n" />

Or, if the event handler is a method:

<MyButton @increase-by="increaseCount" />

Then the value will be passed as the first parameter of that method:

methods: {
increaseCount(n) {
this.count += n
}
}

A component can explicitly declare the events it will emit using the emits option:

export default {
emits: ['inFocus', 'submit']
}

The emits option and defineEmits() macro also support an object syntax. If using TypeScript you can type arguments, which allows us to perform runtime validation of the payload of the emitted events:

export default {
emits: {
submit(payload: { email: string, password: string }) {
// return `true` or `false` to indicate
// validation pass / fail
}
}
}

See also Typing components Emit TS

Although optional, it is recommended to define all emitted events in order to better document how a component should work. It also allows Vue to exclude known listeners from fallthrough attributes, avoiding edge cases caused by DOM events manually dispatched by 3rd party code.

Similar to prop type validation, an emitted event can be validated if it is defined with the object syntax instead of the array syntax.

To add validation, the event is assigned a function that receives the arguments passed to the this.$emit call and returns a boolean to indicate whether the event is valid or not.

export default {
emits: {
// No validation
click: null,
// Validate submit event
submit: ({ email, password }) => {
if (email && password) {
return true
} else {
console.warn('Invalid submit event payload!')
return false
}
}
},
methods: {
submitForm(email, password) {
this.$emit('submit', { email, password })
}
}
}