简体   繁体   English

错误“属性'innerText'在'EventTarget'类型上不存在”?

[英]error “ Property 'innerText' does not exist on type 'EventTarget' ”?

I have an error: 我有一个错误:

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

I'm trying to add event listener and get value from element. 我正在尝试添加事件侦听器并从元素中获取价值。 Everything works fine but this error shows up in console . 一切正常,但此错误显示在控制台中。

public componentDidMount() {
  const element = document.querySelector(".mdc-list")
  element.addEventListener("click", (e) => {
    this.data.menu.title = e.target.innerText
  })
}

You can either make a type guard to narrow down the event target type. 您可以使用类型保护来缩小事件目标的类型。

Or just cast the event target to whichever element you're getting as a target: 或者只是将事件目标强制转换为您要作为目标获取的任何元素:

this.data.menu.title = <HTMLInputElement>(e.target).innerText;

It is a TypeScript issue, cast the event.target to it' type to tell TypeScript that it has the property you set for it. 这是一个TypeScript问题,将event.target强制转换为它的类型,以告诉TypeScript它具有您为其设置的属性。

const input = event.target as HTMLElement;
this.data.menu.title=input.innerText

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

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