簡體   English   中英

KeyboardEvent:類型'EventTarget'上不存在屬性'value'

[英]KeyboardEvent: Property 'value' does not exist on type 'EventTarget'

我從Angular的頁面復制了一些代碼,可視代碼顯示錯誤:

此行的錯誤:

map((e: KeyboardEvent) => e.target.value),

錯誤

Property 'value' does not exist on type 'EventTarget'.

來自Angular 網站的代碼:

import { fromEvent } from 'rxjs';
import { ajax } from 'rxjs/ajax';
import { map, filter, debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators';

const searchBox = document.getElementById('search-box');

const typeahead = fromEvent(searchBox, 'input').pipe(
  map((e: KeyboardEvent) => e.target.value),
  filter(text => text.length > 2),
  debounceTime(10),
  distinctUntilChanged(),
  switchMap(() => ajax('/api/endpoint'))
);

typeahead.subscribe(data => {
 // Handle the data from the API
});

由於,Typescript無法推斷出事件類型。 您需要明確地寫出作為目標的HTMLElement的類型。 例如,如果其類型是HTMLInputElement那么您可以編寫以下行:

map((e: KeyboardEvent) => (<HTMLInputElement>e.target).value),

這將解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM