希望大家都过得好。请问有人可以清楚地解释一下,pipe和subscribe之间在选择时应该如何考虑?为什么?不仅要获取响应,还需要初始化其他变量或更改布尔变量。
同时,请看一下代码。这是正确的做法吗?
public getAlbums(){
this.enableLoader = false;
this.albumHttpService.getAlbums()
.pipe(
map((response) => {
this.enableLoader = false;
if (!response.albums.length) {
this.noDataSet = true;
if (response.albums === null) {
this.list = [];
}
}
else{
this.noDataSet = false;
this.list = response.albums;
}
}),
catchError((error) => {
this.noDataSet = false;
this.data = [];
throw new Error(error);
})
)
.subscribe();
}
感谢您的提前帮助。
pipe
允许您链接多个rxjs操作符,而subscribe
则如其名称所示从可观察对象中检索数据。 - Nicholas K