英文:
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 :
<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 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="mouseDown(day.index, hour, temperature_mode)"
更改为 @mousedown="mouseDown(day.index, hour, temperature_mode, $event)"
,现在我可以访问事件对象了。
英文:
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="mouseDown(day.index, hour, temperature_mode)"
to this @mousedown="mouseDown(day.index, hour, temperature_mode, $event)"
and it I can access the event object now.
Doc:
https://v2.vuejs.org/v2/guide/events.html#Methods-in-Inline-Handlers
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论