嗨,我的主题订阅和搜索电话有问题。我想取消上一个通话以支持当前通话。我已经搜索了以前的线程,但没有成功找到答案。
我知道我应该使用 switchMap() 但我没有成功。无论状态如何,它都会继续所有呼叫。我认为这可能与我设置的方式有关,因为我没有返回我设置的响应。所以没有单一的可观察参考..?
感谢所有帮助!
请看下面的代码:
ngOnInit() {
// I subscribe to the Subject Observable here
this._searchService.quickSearch$
.pipe(
debounceTime(1000),
distinctUntilChanged()
)
.subscribe(
// when value has changed I call runSearch
(queryString) => {this.runSearch(queryString);
}
);
}
运行搜索:
runSearch(searchString: any) {
this.quickSearch.runSearch(searchString).pipe(
//Not working as expected
switchMap(() => {
console.log('switchMap has bee fired');
return this.quickSearch.runSearch(searchString);
})
).subscribe(
(response) => {
// set the two way bind here
this.apiResponse = response;
},
(error) => {
console.log('ERROR!!!');
},
() => {
// this is fired when the observable is closed
console.log('I have been unsubscribed');
}
);
}
快速搜索服务:
runSearch(search: string): Observable<QuickSearch[]> {
...
return this.http.get<QuickSearch[]>(this.api.url, { params: param, headers: header })
.pipe(
map((data: any) => {
return data.map((item: any[]) => this.adapter.adapt(item));
}
),
catchError(error => error)
);
}
谢谢
更新
我仍然没有找到这个问题的答案。因此,我将尝试改写它。
我对此有 5 个部分:
Input box ([])->
rxjs-Subject (input-text)->
runSearch(input-text) -> [ handles response ]
_service.runSearch(input-text) ->
http().get(input-text) => response
当输入框更改时,调用运行搜索,其中也订阅了搜索服务这不会返回