Component Events
This page assumes you've already read the Components Basics. Read that first if you are new to components.
Emitting and Listening to Events
A component can emit custom events directly in template expressions (e.g. in a v-on
handler) using the built-in $emit
method:
template
<!-- MyComponent -->
<button @click="$emit('someEvent')">click me</button>
The parent can then listen to it using v-on
:
template
<MyComponent @some-event="callback" />
The .once
modifier is also supported on component event listeners:
template
<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.
TIP
Unlike native DOM events, component emitted events do not bubble. You can only listen to the events emitted by a direct child component. If there is a need to communicate between sibling or deeply nested components, use an external event bus or a global state management solution.
Event Arguments
It's sometimes useful to emit a specific value with an event. For example, we may want the <BlogPost>
component to be in charge of how much to enlarge the text by. In those cases, we can pass extra arguments to $emit
to provide this value:
template
<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:
template
<MyButton @increase-by="(n) => count += n" />
Or, if the event handler is a method:
template
<MyButton @increase-by="increaseCount" />
Then the value will be passed as the first parameter of that method:
js
function increaseCount(n) {
count.value += n
}
TIP
All extra arguments passed to $emit()
after the event name will be forwarded to the listener. For example, with $emit('foo', 1, 2, 3)
the listener function will receive three arguments.
Declaring Emitted Events
Emitted events can be explicitly declared on the component via the defineEmits()
macro :
vue
<script setup>
defineEmits(['inFocus', 'submit'])
</script>
The $emit
method that we used in the <template>
isn't accessible within the <script setup>
section of a component, but defineEmits()
returns an equivalent function that we can use instead:
vue
<script setup>
const emit = defineEmits(['inFocus', 'submit'])
function buttonClick() {
emit('submit')
}
</script>
The defineEmits()
macro cannot be used inside a function, it must be placed directly within <script setup>
, as in the example above.
If you're using an explicit setup
function instead of <script setup>
, events should be declared using the emits
option, and the emit
function is exposed on the setup()
context:
js
export default {
emits: ['inFocus', 'submit'],
setup(props, ctx) {
ctx.emit('submit')
}
}
As with other properties of the setup()
context, emit
can safely be destructured:
js
export default {
emits: ['inFocus', 'submit'],
setup(props, { emit }) {
emit('submit')
}
}
The emits
option also supports an object syntax, which allows us to perform runtime validation of the payload of the emitted events:
vue
<script setup>
const emit = defineEmits({
submit(payload) {
// return `true` or `false` to indicate
// validation pass / fail
}
})
</script>
If you are using TypeScript with <script setup>
, it's also possible to declare emitted events using pure type annotations:
vue
<script setup lang="ts">
const emit = defineEmits<{
(e: 'change', id: number): void
(e: 'update', value: string): void
}>()
</script>
More details: Typing Component Emits
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.
TIP
If a native event (e.g., click
) is defined in the emits
option, the listener will now only listen to component-emitted click
events and no longer respond to native click
events.
Events Validation
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 emit
call and returns a boolean to indicate whether the event is valid or not.
vue
<script setup>
const emit = defineEmits({
// No validation
click: null,
// Validate submit event
submit: ({ email, password }) => {
if (email && password) {
return true
} else {
console.warn('Invalid submit event payload!')
return false
}
}
})
function submitForm(email, password) {
emit('submit', { email, password })
}
</script>
Usage with v-model
Custom events can also be used to create custom inputs that work with v-model
. Let's revisit how v-model
is used on a native element:
template
<input v-model="searchText" />
Under the hood, the template compiler expands v-model
to the more verbose equivalent for us. So the above code does the same as the following:
template
<input
:value="searchText"
@input="searchText = $event.target.value"
/>
When used on a component, v-model
instead expands to this:
template
<CustomInput
:modelValue="searchText"
@update:modelValue="newValue => searchText = newValue"
/>
For this to actually work though, the <CustomInput>
component must do two things:
- Bind the
value
attribute of a native<input>
element to themodelValue
prop - When a native
input
event is triggered, emit anupdate:modelValue
custom event with the new value
Here's that in action:
vue
<!-- CustomInput.vue -->
<script setup>
defineProps(['modelValue'])
defineEmits(['update:modelValue'])
</script>
<template>
<input
:value="modelValue"
@input="$emit('update:modelValue', $event.target.value)"
/>
</template>
Now v-model
should work perfectly with this component:
template
<CustomInput v-model="searchText" />
Another way of implementing v-model
within this component is to use a writable computed
property with both a getter and a setter. The get
method should return the modelValue
property and the set
method should emit the corresponding event:
vue
<!-- CustomInput.vue -->
<script setup>
import { computed } from 'vue'
const props = defineProps(['modelValue'])
const emit = defineEmits(['update:modelValue'])
const value = computed({
get() {
return props.modelValue
},
set(value) {
emit('update:modelValue', value)
}
})
</script>
<template>
<input v-model="value" />
</template>
v-model
arguments
By default, v-model
on a component uses modelValue
as the prop and update:modelValue
as the event. We can modify these names passing an argument to v-model
:
template
<MyComponent v-model:title="bookTitle" />
In this case, the child component should expect a title
prop and emit an update:title
event to update the parent value:
vue
<!-- MyComponent.vue -->
<script setup>
defineProps(['title'])
defineEmits(['update:title'])
</script>
<template>
<input
type="text"
:value="title"
@input="$emit('update:title', $event.target.value)"
/>
</template>
Multiple v-model
bindings
By leveraging the ability to target a particular prop and event as we learned before with v-model
arguments, we can now create multiple v-model bindings on a single component instance.
Each v-model will sync to a different prop, without the need for extra options in the component:
template
<UserName
v-model:first-name="first"
v-model:last-name="last"
/>
vue
<script setup>
defineProps({
firstName: String,
lastName: String
})
defineEmits(['update:firstName', 'update:lastName'])
</script>
<template>
<input
type="text"
:value="firstName"
@input="$emit('update:firstName', $event.target.value)"
/>
<input
type="text"
:value="lastName"
@input="$emit('update:lastName', $event.target.value)"
/>
</template>
Handling v-model
modifiers
When we were learning about form input bindings, we saw that v-model
has built-in modifiers - .trim
, .number
and .lazy
. In some cases, you might also want the v-model
on your custom input component to support custom modifiers.
Let's create an example custom modifier, capitalize
, that capitalizes the first letter of the string provided by the v-model
binding:
template
<MyComponent v-model.capitalize="myText" />
Modifiers added to a component v-model
will be provided to the component via the modelModifiers
prop. In the below example, we have created a component that contains a modelModifiers
prop that defaults to an empty object:
vue
<script setup>
const props = defineProps({
modelValue: String,
modelModifiers: { default: () => ({}) }
})
defineEmits(['update:modelValue'])
console.log(props.modelModifiers) // { capitalize: true }
</script>
<template>
<input
type="text"
:value="modelValue"
@input="$emit('update:modelValue', $event.target.value)"
/>
</template>
Notice the component's modelModifiers
prop contains capitalize
and its value is true
- due to it being set on the v-model
binding v-model.capitalize="myText"
.
Now that we have our prop set up, we can check the modelModifiers
object keys and write a handler to change the emitted value. In the code below we will capitalize the string whenever the <input />
element fires an input
event.
vue
<script setup>
const props = defineProps({
modelValue: String,
modelModifiers: { default: () => ({}) }
})
const emit = defineEmits(['update:modelValue'])
function emitValue(e) {
let value = e.target.value
if (props.modelModifiers.capitalize) {
value = value.charAt(0).toUpperCase() + value.slice(1)
}
emit('update:modelValue', value)
}
</script>
<template>
<input type="text" :value="modelValue" @input="emitValue" />
</template>
For v-model
bindings with both argument and modifiers, the generated prop name will be arg + "Modifiers"
. For example:
template
<MyComponent v-model:title.capitalize="myText">
The corresponding declarations should be:
js
const props = defineProps(['title', 'titleModifiers'])
defineEmits(['update:title'])
console.log(props.titleModifiers) // { capitalize: true }