Listening to an event from a custom directive #8075
Replies: 2 comments
-
I've run into a similar issue in our project, so I'm also i need of a way to do this. |
Beta Was this translation helpful? Give feedback.
-
Another example of this that I've run into is a v-bubble directive. The directive allows you to bubble custom events from child components up to the parent components. This again relyied on the $on function in order to listen to them. Now that we can't listen to custom events via javascript I can't see how to re-implement this feature. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello! I'm currently trying to replace a very repetitive pattern with a custom directive, using Vue 3, Composition API, and Typescript.
I often have a component where a
CustomButton
should open aCustomDialog
, both placed in a parent component. For now, I keep listening to theclick
on the CustomButton just to open the dialog, so I need to create a function, bind it to theclick
event of the CustomButton, just to call aopenDialog
on the CustomDialog. As this happen very often, I wanted to create a directive that would be bound to the Button, and with an argument, automatically open the dialog with the attached argument.To do so, I made the following inside the
main.ts
:and setting the directive on my CustomButton like this:
My issue is : how to listen to the
click
event emitted by the button, inside the directive, so I could do something like this:Things to consider:
addEventListener
because it's for JS events, and not for custom vue events (if one day I rename theclick
event toclonck
, it won't work for example)$on
is no longer available to listening to vue events, and I don't think that my situation can be managed by props/provide/inject or Pinia stores.I also should mention that in Cypress, there is a way of passing a custom event listener by props, like so:
but I did not find a way to pass props to the
binding.instance
of the directive.Any help would be greatly appreciated! 🙂
Beta Was this translation helpful? Give feedback.
All reactions