Angular2事件是什么Typescript类型


89

如果我在html文件中有以下按钮

<button (click)="doSomething('testing', $event)">Do something</button>

另外,在相应的组件中,我具有此功能

doSomething(testString: string, event){
    event.stopPropagation();
    console.log(testString + ': I am doing something');
}

是否应该为$event输入分配适当的类型?事件参数本身是一个对象,但是如果我将其分配给类型对象,则会收到错误消息

类型对象不存在属性“ stopPropogation”

那么,Typescript考虑$event输入什么呢?


5
doSomething(testString: string, event: MouseEvent)
埃里克·马丁内斯

1
埃里克·马丁内斯(Eric Martinez):您可以发表评论作为答案吗?那清除了我得到的所有标志,所以我认为它是正确的。
Alex J

Answers:


74

正如@AlexJ建议的那样

您传递的事件$event是DOM事件,因此可以使用EventName作为类型。

在您的情况下,此事件为MouseEvent,而文档说:

所述的MouseEvent接口表示用户利用指示设备(如鼠标)的交互的发生是由于事件。使用此界面的常见事件包括click,dblclick,mouseup,mousedown

在所有这些情况下,您都会得到一个MouseEvent

另一个例子:如果您有此代码

<input type="text" (blur)="event($event)"

事件触发时,您会收到一个FocusEvent

因此,您可以非常简单地完成操作,控制台记录事件,您将看到一条类似于该消息的消息,我们将获得事件名称

FocusEvent {isTrusted: true, relatedTarget: null, view: Window, detail: 0, which: 0…}

您可以随时访问文档以获取现有活动的列表。

编辑

您还可以检查dom.generated.d.ts带有所有类型输入的TypeScript 。您的情况stopPropagation()是的一部分Event,由扩展MouseEvent


有关HTMLInputEvent类型的信息,请参见此答案
hlovdal

1
应当注意,如果事件通过Angular组件@Output(通常是type的组件字段)起源于Angular组件EventEmitter<T>,则$event参数的类型为T。参见angular.io/api/core/EventEmitter
jfroy

26

一些常用事件及其相关名称(MouseEventFocusEventTouchEventDragEventKeyboardEvent):

| MouseEvent | FocusEvent |  TouchEvent | DragEvent | KeyboardEvent |
|:----------:|:----------:|:-----------:|:---------:|:-------------:|
|    click   |    focus   |  touchstart |    drag   |    keypress   |
|   mouseup  |    blur    |  touchmove  |    drop   |     keyup     |
| mouseleave |   focusin  | touchcancel |  dragend  |    keydown    |
|  mouseover |  focusout  |   touchend  |  dragover |               |

通用事件与:

  • 更改
  • 无效
  • 重启
  • 滚动
  • 选择
  • 提交
  • 拨动
  • 等候

如果您在Typescript的存储库中进行挖掘,则dom.generation.d.ts提供了一个全局事件接口(信贷转到Eric的答案),您可以在第5725行找到所有事件处理程序的映射:

interface GlobalEventHandlersEventMap {
  "abort": UIEvent;
  "animationcancel": AnimationEvent;
  "animationend": AnimationEvent;
  "animationiteration": AnimationEvent;
  "animationstart": AnimationEvent;
  "auxclick": MouseEvent;
  "blur": FocusEvent;
  "cancel": Event;
  "canplay": Event;
  "canplaythrough": Event;
  "change": Event;
  "click": MouseEvent;
  "close": Event;
  "contextmenu": MouseEvent;
  "cuechange": Event;
  "dblclick": MouseEvent;
  "drag": DragEvent;
  "dragend": DragEvent;
  "dragenter": DragEvent;
  "dragexit": Event;
  "dragleave": DragEvent;
  "dragover": DragEvent;
  "dragstart": DragEvent;
  "drop": DragEvent;
  "durationchange": Event;
  "emptied": Event;
  "ended": Event;
  "error": ErrorEvent;
  "focus": FocusEvent;
  "focusin": FocusEvent;
  "focusout": FocusEvent;
  "gotpointercapture": PointerEvent;
  "input": Event;
  "invalid": Event;
  "keydown": KeyboardEvent;
  "keypress": KeyboardEvent;
  "keyup": KeyboardEvent;
  "load": Event;
  "loadeddata": Event;
  "loadedmetadata": Event;
  "loadend": ProgressEvent;
  "loadstart": Event;
  "lostpointercapture": PointerEvent;
  "mousedown": MouseEvent;
  "mouseenter": MouseEvent;
  "mouseleave": MouseEvent;
  "mousemove": MouseEvent;
  "mouseout": MouseEvent;
  "mouseover": MouseEvent;
  "mouseup": MouseEvent;
  "pause": Event;
  "play": Event;
  "playing": Event;
  "pointercancel": PointerEvent;
  "pointerdown": PointerEvent;
  "pointerenter": PointerEvent;
  "pointerleave": PointerEvent;
  "pointermove": PointerEvent;
  "pointerout": PointerEvent;
  "pointerover": PointerEvent;
  "pointerup": PointerEvent;
  "progress": ProgressEvent;
  "ratechange": Event;
  "reset": Event;
  "resize": UIEvent;
  "scroll": Event;
  "securitypolicyviolation": SecurityPolicyViolationEvent;
  "seeked": Event;
  "seeking": Event;
  "select": Event;
  "selectionchange": Event;
  "selectstart": Event;
  "stalled": Event;
  "submit": Event;
  "suspend": Event;
  "timeupdate": Event;
  "toggle": Event;
  "touchcancel": TouchEvent;
  "touchend": TouchEvent;
  "touchmove": TouchEvent;
  "touchstart": TouchEvent;
  "transitioncancel": TransitionEvent;
  "transitionend": TransitionEvent;
  "transitionrun": TransitionEvent;
  "transitionstart": TransitionEvent;
  "volumechange": Event;
  "waiting": Event;
  "wheel": WheelEvent;
}

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.