Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/

like image 211
dsat Avatar asked Dec 18 '17 19:12

dsat


1 Answers

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)
like image 89
Bert Avatar answered Oct 23 '22 15:10

Bert