dsat
dsat

Reputation: 147

How to pass the event target as $emit argument in VueJS2?

I have this VueJS 2 template

    var aThing = Vue.component('something',{
    template :` <button @click="$emit('custom-event','hello there')">Click me</button>`});

Is it possible to pass the button that was actually pushed as an argument to $emit ? For example in the click event it is usually passed but the event and can be accesed in a function like this

function(event){
  event.target; //I want this
}

Here is a jsfiddle of my issue

https://jsfiddle.net/wntzv4sk/2/

Upvotes: 8

Views: 2999

Answers (1)

Bert
Bert

Reputation: 82469

Vue makes the event object available in the template via a variable called $event. This is documented here.

That being the case, you could emit the target of the event in this manner:

$emit('custom-event', 'hello-there', $event.target)

Upvotes: 10

Related Questions