【发布时间】:2020-07-17 06:50:58
【问题描述】:
const apiData = ajax('/api/data').pipe(
retry(3), // Retry up to 3 times before failing
map(res => {
if (!res.response) {
throw new Error('Value expected!');
}
return res.response;
}),
catchError(err => of([]))
);
我正在学习 angular 并且从 angular 官方网站我看到了这段代码。 .pipe() 函数是干什么用的?
【问题讨论】:
-
异步模式下可观察的返回数据,您可以更改此响应,例如您希望收到类似
data:{prop1:'prop1',array:[0,1,2]}的内容,而您只想要“array”的值,因此您使用“map”来转换响应:pipe(map(res=>res.array))从 Rxjs 6 开始,您使用 pipe(operator1(res),operator2(res) ,....),其中 operator# 是不同的运算符 (swichMap,map,takeWhile,concat,bitwise...)。结果是一个新的 Observable,当我们订阅它时返回我们想要的响应 -
这能回答你的问题吗? What is pipe() function in Angular
标签: angular rxjs reactive-programming