typescript 打字稿中的 HTMLElement.addEventListener 示例?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42796613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:37:23  来源:igfitidea点击:

example of HTMLElement.addEventListener in typescript?

typescript

提问by Mike_G

can I get one? this is what I have, but I'm getting a red squiggly line:

我可以得到一个吗?这就是我所拥有的,但我得到了一条红色的波浪线:

let button = <HTMLElement>document.body.querySelector(".btn");
button.addEventListener("click", () =>{});//not sure what to do here.  I know its wrong though.

below is my tsonconfig

下面是我的 tsonconfig

{
    "compileOnSave": true,
    "compilerOptions": {
        "noImplicitAny": true,
        "removeComments": true,
        "sourceMap": false,
        "target": "es5",
        "lib": [
            "es6",
            "dom"
        ]
    },
    "include": [ "**/*" ]
}

采纳答案by Mike_G

The issue was ReSharper. I disabled the plugin and it worked fine. For reference I have attached two picutres, the first with ReSharper enabled, and the second with it disabled.resharpernoreshaper

问题是 ReSharper。我禁用了插件,它工作正常。作为参考,我附上了两张图片,第一张启用了 ReSharper,第二张禁用了它。锐化器整形者

回答by basarat

With your given tsconfig.json it compiles fine:

使用您给定的 tsconfig.json,它可以正常编译:

let button = <HTMLElement>document.body.querySelector(".btn");
button.addEventListener("click", () => { });

enter image description here

在此处输入图片说明

Your IDE is lying. Works fine in VSCode / alm

你的 IDE 在说谎。在 VSCode / alm 中工作正常