2 回答

TA贡献2051条经验 获得超10个赞
我更喜欢使用拆解方法。它不会污染数据操作的管道链,并且在处理错误后也会执行:.add()
this.userService.getMerchantsBySearch(searchTerm)
.pipe(
// manipulate data here
)
.subscribe(
res => {
// handle final data
},
error => {
// handle error
}
).add(() => {
// teardown here (e.g. this.isLoading=false;)
});
顺便说一句:如果 是可观察的,则无法在订阅回调内分配它。您需要将其分配给您正在使用的用户服务方法的响应。请注意不要在流上调用 subscribe,因为它将返回订阅而不是可观察量:this.people$
this.people$ = this.userService.getMerchantsBySearch(searchTerm).pipe(...)

TA贡献1786条经验 获得超13个赞
你可以实际它并添加一个pipefinalize
this.userService.getMerchantsBySearch(searchTerm)
.pipe(finalize(() => this.isLoading=false))
.subscribe(
res => {
this.people$ =res;
});
添加回答
举报