How can I consume EventEmitter outside of a component in vue 3?

65 Views Asked by At

I'm trying to make a wrapper for my vuetify dialog component that functions like a confirm box so that I can call it like a confirm box in vanilla javascript. It will be backed by a store so I know I can use a watch to review a boolean that receives the user response, but for increased performance I'd like to use the EventBus to subscribe to the event within the wrapper outside of the component.

The end result would be like:

if (confirmDialog('Reset Form', 'Would you like to reset this form?')) {
  // if user selects yes then do stuff...
}

confirmDialog would then be defined in a utility file and return a boolean to represent the user response.

In the ConfirmDialog.vue file I have a button that emits:

emit('user-confirm-response', true, null);

How do I subscribe to and await the result of this response outside of a component so that the confirmDialog method returns the users response? I'm looking for a way to await the result of 'user-confirm-response' event, something similar to await once in node.js.

0

There are 0 best solutions below