我在Angular 2组件代码中使用的是TypeScript Version 2。

我得到错误“属性“值”不存在类型“EventTarget”下面的代码,什么可能是解决方案。谢谢!

e.target.value.match(/\S+/g) || []).length

import { Component, EventEmitter, Output } from '@angular/core';

@Component({
  selector: 'text-editor',
  template: `
    <textarea (keyup)="emitWordCount($event)"></textarea>
  `
})
export class TextEditorComponent {
  @Output() countUpdate = new EventEmitter<number>();

  emitWordCount(e: Event) {
    this.countUpdate.emit(
            (e.target.value.match(/\S+/g) || []).length);
  }
}

当前回答

心理图像11 +。

tsconfig开放。禁用strictTemplates。

 "angularCompilerOptions": {
    ....
    ........
    "strictTemplates": false
  }

其他回答

角10 +

tsconfig开放。禁用strictDomEventTypes。

  "angularCompilerOptions": {
    ....
    ........
    "strictDomEventTypes": false
  }

应该使用currentValue,因为currentValue的类型是EventTarget & HTMLInputElement。

考虑任何美元()

<textarea (keyup)="emitWordCount($any($event))"></textarea>
fromEvent<KeyboardEvent>(document.querySelector('#searcha') as HTMLInputElement , 'keyup')
    .pipe(
      debounceTime(500),
      distinctUntilChanged(),
      map(e  => {
            return e.target['value']; // <-- target does not exist on {}
        })
    ).subscribe(k => console.log(k));

也许上面的方法会有所帮助。 根据实际代码更改它。 问题是........目标(“价值”)

心理图像11 +。

tsconfig开放。禁用strictTemplates。

 "angularCompilerOptions": {
    ....
    ........
    "strictTemplates": false
  }