We are currently working with single file Vue components and we're facing a challenge in our mousemove event handler. We want to be able to determine if the target element is clickable.
Within our Vue templates, we utilize v-on directives such as:
v-on:click="someCallback"
.
Unfortunately, it seems difficult to easily identify whether an event listener has been registered for a specific element (e.g., via a v-on directive).
To address this issue, we are considering adding a custom attribute to elements with a v-on:click directive, like "clickable". However, we need this process to occur automatically.
This may involve creating a custom directive that wraps Vue's native "on" directive or finding a way to integrate into Vue's rendering cycle. Both approaches present challenges as we couldn't locate the directive on either the Vue instance or the Vue component object.
Our attempts so far include:
- Attempting to extract information about registered listeners from the target element within the event object passed to the event handler. Unfortunately, it appears that browsers do not offer this information.
- Searching through the Vue component object for any data structure that tracks which event listener corresponds to which element and handler. Regrettably, we were unable to locate this information even though it should theoretically exist somewhere.
If anyone has a clever idea on how we can automate the addition of a custom attribute to elements with a v-on:click directive, we would greatly appreciate it!
Thank you!
EDIT:
For example, we have:
<div id="x" @click="someMethod" />
in our template.
However, we wish to automatically include a custom attribute (without manual intervention in countless cases):
<div id="x" clickable @click="someMethod" />
In the event handler for addEventListener('mousemove', handler), we could then check for this attribute: if (e.target.hasAttribute('clickable'))
Any alternative methods to achieve this functionality (i.e., determining if an element is clickable during the mousemove handler) would be welcomed as well.