Mic*_*lis 2 observable rxjs angular
我有一个 HTTP 请求
searchService() {
return this.httpClient.get(URL);
}
Run Code Online (Sandbox Code Playgroud)
我是这样订阅的
search() {
this.service.searchService().subscribe((data) => { });
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,这需要几秒钟的时间,如果有人使用不同的搜索键多次单击,结果取决于请求的速度。
所以,我想取消所有以前的订阅并只保留最后的搜索
您将需要使用一些 rxjs 运算符,即debounceTime,distinctUntilChanged和switchMap以确保您不会发送太多 API 调用,并且仅获得最新请求的结果。
查看本文以获取详细示例:
\n https://alligator.io/angular/real-time-search-angular-rxjs/