该运算符负责通过返回新的Observable或错误来在源Observable上捕获错误。
语法
catchError(selector_func: (err_func: any, caught: Observable) => O):Observable
参量
selector_funct-选择器函数接受2个参数,错误函数和catch参数,这是一个可观察的。
返回值
它根据selector_func发出的值返回一个observable。
例
import { of } from 'rxjs';
import { map, filter, catchError } from 'rxjs/operators';
let all_nums = of(1, 6, 5, 10, 9, 20, 40);
let final_val = all_nums.pipe(
map(el => {
if (el === 10) {
throw new Error("Testing catchError.");
}
return el;
}),
catchError(err => {
console.error(err.message);
return of("From catchError");
})
);
final_val.subscribe(
x => console.log(x),
err => console.error(err),
() => console.log("Task Complete")
);输出

作者:terry,如若转载,请注明出处:https://www.web176.com/rxjs/1951.html
支付宝
微信