我對如何將.subscribe
函數與.do
函數結合使用產生誤解?「訂閱」類型中不存在屬性'do'
這是我觀察到的序列:
lookupSubscriber = (text$: Observable<string>) =>
text$.debounceTime(300)
.distinctUntilChanged()
.do(() => this.searching = true)
.switchMap(term => {
var data = this._callApi(this.lookupSubscriberAPI, term)
.do(() => {
this.searchFailed = false;
this.searching = false;
})
.catch(() => {
this.searchFailed = true;
this.searching = false;
return Observable.of([]);
})
return data;
})
.do(() => this.searching = false);
如果我_callApi功能如下所示,它的工作原理:
_callApi(url: string, term: string) {
if (term === '') {
return of.call([]);
}
return map.call(this.dataService.get(url + term), response => {
var data = this._transformSubscriberInfo(response);
return data;
})
}
然而,當我嘗試用重寫一遍subscribe
功能如下:
_callApi = (url: string, term: string) => {
return this.dataService.get(url + term)
.subscribe(
response => { this._transformSubscriberInfo(response) },
error => error.text(),
() => {
if (Logging.isEnabled.light) {
console.log('%c API Call Complete', Logging.normal.orange);
}
))
}
...然後將數據調用成功,但我收到錯誤:Property 'do' does not exist on type 'Subscription'.
基本上我試圖抓住錯誤和運行API調用後的「永遠」功能,如圖中的_callApi
第二個版本。