0 day streak
javascript / beginner
Snippet

Emitting Custom Events

Use defineEmits to declare custom events that a child component can send to its parent. The emit function triggers the event with optional data.

snippet.js
javascript
1
2
3
4
5
const emit = defineEmits(['notify']);
function sendAlert() {
emit('notify', 'Action completed!');
}
vue
Breakdown
1
const emit = defineEmits(['notify']);
Declares that this component can emit an event named 'notify'.
2
emit('notify', 'Action completed!');
Triggers the event and passes a string as a payload.