我已经从Angular的页面中复制了一些代码,可视代码显示了错误:
此行错误:
map((e: KeyboardEvent) => e.target.value),
错误
Property 'value' does not exist on type 'EventTarget'.
来自角度website的代码:
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
});
答案 0 :(得分:2)
因为,Typescript无法推断事件类型。您需要显式编写目标HTMLHTMLElement的类型。例如,如果其类型为HTMLInputElement
,则可以编写以下行:
map((e: KeyboardEvent) => (<HTMLInputElement>e.target).value),
这将解决您的问题。