我正在尝试创建一个自动完成功能,为每次输入从 API 接收数据,现在我每次都会显示数据,但首先我总是在每种类型上出现此错误。
您在需要流的地方提供了“未定义”。您可以提供 Observable、Promise、Array 或 Iterable。
即使我将数据订阅到变量中,也没有任何反应。这是错误的图像,您将看到显示的数据
这是我的 app.component.ts
ngOnInit() {
this.results = this.searchText.valueChanges.pipe(
startWith(''),
// delay emits
debounceTime(300),
switchMap(value => {
if (value !== '' && value !== undefined) {
this.lookup(value).subscribe((value: any) => {
console.log('data sub:', value);
});
} else {
return of(null);
}
})
);
}
lookup(value: string): Observable<any> {
return this.appService.autoComplete(value.toLowerCase()).pipe(
map(results => results.hits.hits)
);
}
Run Code Online (Sandbox Code Playgroud)
这是我的服务。ts
public autoComplete(name: string): Observable<Response> {
const params = new HttpParams()
.set('name', name);
return this.httpClient.get<Response>(this.host, { params});
}
Run Code Online (Sandbox Code Playgroud)
这也是我的 html: …