Back
VueEasyNew

How do you emit custom events from a child to a parent component?

defineEmits macro

Ideal Answer

Use the defineEmits macro to declare events, then call the returned function to emit them:

JavaScript
<script setup>
const emit = defineEmits(['update', 'delete'])

function handleClick() {
  emit('update', { id: 1 })
}
</script>

The parent listens with v-on (@):

JavaScript
<ChildComponent @update="onUpdate" />

You can also validate emitted payloads by using the object syntax instead of an array.