$emit does not add event vue3

67 views Asked by At

I have the following element, which listens on the drawend event.

<ol-interaction-draw
  ref="test"
  @drawend="drawend"
>

When drawend is triggered normally by the element itself, event is passed to the function:

drawend(event) {
  console.log(event);
}

But when I try to trigger it manually, event is undefined.

test = ref(null);
test.$emit("drawend");

How can I emit the event correctly?

1

There are 1 answers

1
Oleg Barabanov On

When you call an event like this:

<ol-interaction-draw
  ref="test"
  @drawend="drawend"
>

in fact, you are calling:

<ol-interaction-draw
  ref="test"
  @drawend="$emit('drawend', $event)"
>

Where $emit has a second parameter - the event object or any other data.

Try to do this, for example, and you will see for yourself what will be transferred to the draw end:

test = ref(null);
test.$emit("drawend", {}); // => where {} is a custom object that is passed to the "drawend" trigger