【问题标题】:What Typescript type is Angular2 event什么 Typescript 类型是 Angular2 事件
【发布时间】:2016-06-28 06:38:49
【问题描述】:

如果我在 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 输入是什么?

【问题讨论】:

  • doSomething(testString: string, event: MouseEvent)
  • Eric Martinez:您能发表您的评论作为答案吗?这清除了我得到的所有标志,所以我认为它是正确的。

标签: javascript typescript angular


【解决方案1】:

根据官方eventObject 类型,在我的情况下,当我将event 类型转换为对象时,它不会抛出任何错误,但在阅读angular2 的文档后发现event 是类型EventEmitter 这样您就可以在 EventEmitter 中输入您的活动种姓

看到这里是相同的http://plnkr.co/edit/8HRA3bM0NxXrzBAjWYXc?p=previewplunkr

更多信息请参考这里https://angular.io/docs/ts/latest/guide/template-syntax.html#!#event-binding

【讨论】:

    【解决方案2】:

    正如@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…}
    

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

    编辑

    您还可以检查 TypeScript dom.generated.d.ts 并移植所有类型。在您的情况下,stopPropagation()Event 的一部分,由MouseEvent 扩展。

    【讨论】:

    • 请参阅this answer 了解 HTMLInputEvent 类型。
    • 需要注意的是,如果事件通过@Output来自Angular组件,通常是EventEmitter&lt;T&gt;类型的组件字段,那么$event参数的类型是T .见angular.io/api/core/EventEmitter
    • 例如。 KeyboardEvent 用于 (keydown)(keypress) 等事件
    【解决方案3】:

    一些常用的事件及其相关名称(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 |               |
    

    通用Event 关联到:

    • 关闭
    • 改变
    • 无效
    • 播放
    • 重置
    • 滚动
    • 选择
    • 提交
    • 切换
    • 等待中

    如果您深入了解Typescript's repository, dom.generated.d.ts provides a global events interface(归功于Eric's answer),您可能会在line 5752 找到所有事件处理程序映射:

    interface GlobalEventHandlersEventMap {
        "abort": UIEvent;
        "animationcancel": AnimationEvent;
        "animationend": AnimationEvent;
        "animationiteration": AnimationEvent;
        "animationstart": AnimationEvent;
        "auxclick": MouseEvent;
        "beforeinput": InputEvent;
        "blur": FocusEvent;
        "cancel": Event;
        "canplay": Event;
        "canplaythrough": Event;
        "change": Event;
        "click": MouseEvent;
        "close": Event;
        "compositionend": CompositionEvent;
        "compositionstart": CompositionEvent;
        "compositionupdate": CompositionEvent;
        "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;
        "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;
    }
    

    【讨论】:

    • 正是我想要的东西,我想要的格式?。谢谢 CPHPython!
    • 很高兴它有帮助,感谢您更新 TS 链接,我也更新了处理程序列表。
    • 这应该是格式化的黄金标准——这样一个令人难以置信的格式和简洁的答案! ⭐
    猜你喜欢
    • 2020-01-02
    • 2019-08-04
    • 2021-01-08
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2021-07-06
    • 2021-08-01
    相关资源
    最近更新 更多