你可以访问Vue.js mousedown事件的Event对象如何?

huangapple go评论56阅读模式
英文:

How can I access the Event object of a mousedown Vuejs event?

问题

I have a Vue component (Vue 2.5) using typescript.

I have a @mousedown event which launches the mouseDown function:

<td
  v-for="hour in hours"
  @mousedown="mouseDown(day.index, hour, temperature_mode)"
  @mouseup="isMouseDown = false"
>
mouseDown(day: number, hour: number, value: number) {
   this.isMouseDown = true;
   this.setScheduledTempMode(day, hour, value);
}

I want to access the event object of this event.

I tried something like this:

mouseDown(e: Event, day: number, hour: number, value: number) {
   this.isMouseDown = true;
   this.setScheduledTempMode(day, hour, value);
   console.log(e);
}

But the log renders the day parameter value.

I also tried:

  • Changing the type to "any," but I got the same result.
  • Moving "e: Event" to the end of parameters; the result of the log is "undefined."

So how can I retrieve the event object?

Is the type not correct?

Thanks

英文:

I have a Vue component (Vue 2.5) using typescript.

I have a @mousedown event which launch mouseDown function :

&lt;td
  v-for=&quot;hour in hours&quot;
  @mousedown=&quot;mouseDown(day.index, hour, temperature_mode)&quot;
  @mouseup=&quot;isMouseDown = false&quot;
&gt;

mouseDown(day: number, hour: number, value: number) {
   this.isMouseDown = true;
   this.setScheduledTempMode(day, hour, value);
}

I want to access the event object of this event.

I tried something like this :

mouseDown(e: Event, day: number, hour: number, value: number) {
   this.isMouseDown = true;
   this.setScheduledTempMode(day, hour, value);
   console.log(e);
}

But the log renders the day parameter value.

I tried also :

  • change the type to "any" but I have the same result.
  • move "e: Event" at the end of parameters, the result of the log is "undefined".

So how can I retrieve the event object?

Is this the type that is not good?

Thanks

答案1

得分: 0

我发现与普通的 JS 不同,事件对象不会在任何事件上默认添加。

因此,我必须在调用方法时使用特殊变量 $event 来访问它。

我将这个 @mousedown=&quot;mouseDown(day.index, hour, temperature_mode)&quot; 更改为 @mousedown=&quot;mouseDown(day.index, hour, temperature_mode, $event)&quot;,现在我可以访问事件对象了。

英文:

I found that contrary to vanilla JS, the event object is not added by default on any events.

So I have to use the special variable $event on my method call to access it.

I've changing this @mousedown=&quot;mouseDown(day.index, hour, temperature_mode)&quot; to this @mousedown=&quot;mouseDown(day.index, hour, temperature_mode, $event)&quot; and it I can access the event object now.

Doc:
https://v2.vuejs.org/v2/guide/events.html#Methods-in-Inline-Handlers

huangapple
  • 本文由 发表于 2023年5月25日 17:54:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定