简体   繁体   English

使用 event.target.id 时抛出的类型“EventTarget”上不存在属性“id”

[英]Property 'id' does not exist on type 'EventTarget' thrown when working with event.target.id

I am attempting to perform a HostListener on my component, specifying that I want something to happen when anything BUT a specific element is clicked;我试图我的组件上执行HostListener,指定我想,但点击任何一个特定的元素时发生的事情; however, I am receiving the error:但是,我收到错误:

Property 'id' does not exist on type 'EventTarget'类型“EventTarget”上不存在属性“id”

The code runs fine in the UI, but I can't shake this error thrown by linting.代码在 UI 中运行良好,但我无法摆脱 linting 引发的这个错误。

I understand that I must use a type for the this, such as if I were performing it about an input element.我知道我必须为此使用一个类型,例如如果我对输入元素执行它。 However I am performing the check on a normal <div> that is being clicked...但是,我正在对正在单击的普通<div>执行检查...

I am unsure of how to proceed.我不确定如何继续。

@HostListener('document:click', [$event'])
myClick(event: MouseClick): void {
  if (event.target.id !== 'some-id') {
    // do something here
  }
}

I have been playing around with something such as:我一直在玩这样的东西:

if (<HTMLElement>event.target.id...

but I cannot find a fix for this...但我找不到解决这个问题的方法......

I will post @ConnorsFan's answer since his comment worked out for me.我会发布@ConnorsFan 的回答,因为他的评论对我有用。

In the end I went with using (<HTMLElement>event.target).id .最后我使用(<HTMLElement>event.target).id

It seems that my attempt at setting event.target.id was misguided as I understand now that the type should be applied to the target, and the ID is just an attribute of said target element.似乎我设置 event.target.id 的尝试被误导了,因为我现在明白该类型应该应用于目标,而 ID 只是所述目标元素的一个属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM