【问题标题】:How to pass the event target as $emit argument in VueJS2?如何在 VueJS2 中将事件目标作为 $emit 参数传递?
【发布时间】:2018-06-01 04:23:15
【问题描述】:

我有这个 VueJS 2 模板

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

是否可以将实际按下的按钮作为参数传递给 $emit ?例如,在click 事件中,它通常被传递,但该事件可以在这样的函数中被访问

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

这是我的问题的 jsfiddle

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

【问题讨论】:

  • $emit('custom-event', 'hello-there', $event.target).
  • @Bert Yeap,似乎就是这样。您想将其发布为答案以便我接受吗?

标签: javascript vue.js vuejs2 vue-component


【解决方案1】:

Vue 通过一个名为$event 的变量使事件对象在模板中可用。这是documented here

既然如此,您可以通过这种方式发出事件的目标:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 2017-02-27
    • 1970-01-01
    相关资源
    最近更新 更多