【发布时间】:2017-07-25 23:22:57
【问题描述】:
我对如何将.subscribe 函数与.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 调用之后运行“always”函数,如_callApi 的第二个版本所示。
【问题讨论】:
标签: javascript angular rxjs observable subscribe