To emit custom events from child components in Vue.js, follow these steps:
methods: {
emitCustomEvent() {
this.$emit('custom-event'); // Emit the 'custom-event'
}
}
<template>
<div>
<child-component @custom-event="handleCustomEvent"></child-component>
</div>
</template>
<script>
export default {
methods: {
handleCustomEvent() {
// Custom event handling logic
console.log('Custom event emitted from child component');
}
}
}
</script>
In the example above, the @custom-event
is the listener for the custom event emitted from the child component. When the event is emitted, the handleCustomEvent
method in the parent component is called.