I have a component which is sometimes being used as a child of a parent and sometimes it does not have any parent.
This component emits a particular event which either being caught by the parent ( if subscribed) or I would like to handle it by this component itself.
So at run time, How do I programmatically check
if (someone is listening to the event){
let them catch and handle it
}else {
let's handle it by our own
}
This allows you to check listeners passed to a component:
this.$listeners;
It returns the parent scope event listeners. See the doc here.
In particular for your use-case:
// To check whether the "myEvent" event is listened to
if (this.$listeners.myEvent){
// let them catch and handle it
} else {
// let's handle it by our own
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With