我使用的是最新版本的 Angular,我收到此错误 属性 'value' does not exist on type 'EventTarget'

I'm using the Lateset version of Angluar and I get this error Property 'value' does not exist on type 'EventTarget'

因此,当我将 $event 与 onkeyup 事件一起使用并且我希望输入字段的值将其传递给过滤器函数时,它不起作用

<div class="container mx-auto p-5">
  <div class="searchBar">
    <div class="field">
      <p class="control has-icons-left">
        <input #filterInput class="input" type="text" (keyup)="filterNotes($event.target.value)" placeholder="Filter">
        <span class="icon is-small is-left">
          <i class="fas fa-search"></i>
        </span>
      </p>
    </div>
  </div>
</div>

class 组件:

filterNotes(query: string) {
    // some logic
}

在这种情况下,我认为 $event.target 没有输入,所以打字稿不知道它是什么类型的目标,请尝试下面的代码:

在 .html 文件中仅传递事件:

...
<input (keyup)="filterNotes($event)">
...

在 .ts 文件中修改您接收事件的方法:

filterNotes(event: Event): string {
  const { value } = event.target as HTMLInputElement // forcing the type
  // the rest of yout logic goes here
}

只需添加到

"angularCompilerOptions":{
    "strictDomEventtypes":false
}

在您的 tsconfig.json 文件中