Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a component has an event listener attached to it

Assuming there's some <Form> component. It can be called with a @cancel event listener attached to it and if it's the case, I want to show the cancel button that triggers this event. If there's no @cancel event, the cancel button should not be visible.

Is there a way to check if a component has event listener attached to it?

Currently I do:

<template>   <form>     <button v-if="cancelEventPassed" @click="$emit('cancel')">Cancel</button>   </form> </template> 

And call it like this:

<Form :cancelEventPassed="true" @cancel="handle_cancel" /> 

either

<Form/> 

Is it possible to achieve this without using any additional property like cancelEventPassed?

like image 889
Kasheftin Avatar asked Oct 12 '17 10:10

Kasheftin


People also ask

How do you check if an element already has an event listener?

To check if an element has event listener on it with JavaScript, we can call the getEventListeners function in the Chrome developer console. getEventListeners(document. querySelector("your-element-selector")); in the Chrome developer console to select the element we want to check with querySelector .

How do you get all event listeners on an element?

When developing a Web site, event listeners are implemented, by third party libraries or by your own. Basically, 2 ways when defining events, just need to add the prefix on using the event attribute : element. addEventListener('click', function() {…}); element.

Can an element have two event listeners?

You can add many event handlers to one element. You can add many event handlers of the same type to one element, i.e two "click" events. You can add event listeners to any DOM object not only HTML elements.


2 Answers

When there are listeners attached to a component they are available in the $listeners property of the component.

You can use that property to determine if a specific listener is available. For example, here is a computed property that checks for the existence of a cancel listener.

computed:{   hasCancelListener(){     return this.$listeners && this.$listeners.cancel   } } 

And here is an example of that used in a component.

console.clear()    Vue.component("CustomForm", {    template:`      <div>        <h1>Custom Form</h1>        <button v-if="hasCancelListener" @click="$emit('cancel')">I have a listener!</button>      </div>    `,    computed:{      hasCancelListener(){        return this.$listeners && this.$listeners.cancel      }    },  })    new Vue({    el: "#app",    methods:{      onCancel(){        alert('canceled')      }    }  })
<script src="https://unpkg.com/[email protected]"></script>  <div id="app">    <custom-form @cancel="onCancel"></custom-form>    <hr>    <custom-form></custom-form>  </div>
like image 151
Bert Avatar answered Oct 03 '22 19:10

Bert


In Vue 3, the $listeners object has been removed. The listeners are now part of the $attrs object and are prefixed with on.

In order to check if a particular listener is present or not in a child component, you can do:

computed: {   hasCancelListener() : boolean {     return (this.$attrs && this.$attrs.onCancel) as boolean   } } 

The child component is called as:

<custom-form @cancel="onCancel"></custom-form> 
like image 32
Prerak Sola Avatar answered Oct 03 '22 20:10

Prerak Sola