RxJS之catchError
Catches errors on the observable to be handled by returning a new observable or throwing an error.
返回新的可观察对象
import { Component, OnInit } from '@angular/core';
import { of } from 'rxjs/observable/of';
import { map, catchError, retry } from 'rxjs/operators';
@Component({
selector: 'app-error',
templateUrl: './error.component.html',
styleUrls: ['./error.component.css']
})
export class ErrorComponent implements OnInit {
constructor() { }
ngOnInit() {
of('Leo', 'Raph', 'Mikey', 'Don').pipe(
map(turtle => {
if (turtle === 'Mikey') {
throw new Error('出错了');
}
return turtle;
}),
catchError(err => of('Aioria', 'Mu'))
).subscribe(turtle => {
console.log(turtle);
});
}
}

继续抛出异常
import { Component, OnInit } from '@angular/core';
import { of } from 'rxjs/observable/of';
import { map, catchError, retry } from 'rxjs/operators';
@Component({
selector: 'app-error',
templateUrl: './error.component.html',
styleUrls: ['./error.component.css']
})
export class ErrorComponent implements OnInit {
constructor() { }
ngOnInit() {
of('Leo', 'Raph', 'Mikey', 'Don').pipe(
map(turtle => {
if (turtle === 'Mikey') {
throw new Error('出错了');
}
return turtle;
}),
catchError(err => {
throw new Error('继续抛出异常');
})
).subscribe(turtle => {
console.log(turtle);
});
}
}

重新尝试
import { Component, OnInit } from '@angular/core';
import { of } from 'rxjs/observable/of';
import { map, catchError, retry } from 'rxjs/operators';
@Component({
selector: 'app-error',
templateUrl: './error.component.html',
styleUrls: ['./error.component.css']
})
export class ErrorComponent implements OnInit {
constructor() { }
ngOnInit() {
of('Leo', 'Raph', 'Mikey', 'Don').pipe(
map(turtle => {
if (turtle === 'Mikey') {
throw new Error('出错了');
}
return turtle;
}),
retry(2),
catchError(err => of('Aioria', 'Mu'))
).subscribe(turtle => {
console.log(turtle);
});
}
}

RxJS之catchError的更多相关文章
- Angular记录(9)
文档资料 箭头函数--MDN:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_fun ...
- Angular6封装http请求
最近抽空学习了一下Angular6,之前主要使用的是vue,所以免不了的也想对Angular6提供的工具进行一些封装,今天主要就跟大家讲一下这个http模块. 之前使用的ajax库是axios,可以设 ...
- 【CuteJavaScript】Angular6入门项目(4.改造组件和添加HTTP服务)
本文目录 一.项目起步 二.编写路由组件 三.编写页面组件 1.编写单一组件 2.模拟数据 3.编写主从组件 四.编写服务 1.为什么需要服务 2.编写服务 五.引入RxJS 1.关于RxJS 2.引 ...
- Angular 从入坑到挖坑 - HTTP 请求概览
一.Overview angular 入坑记录的笔记第四篇,介绍在 angular 中如何通过 HttpClient 类发起 http 请求,从而完成与后端的数据交互. 对应官方文档地址: Angul ...
- angular2 学习笔记 ( rxjs 流 )
RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅, ...
- RxJS 实现摩斯密码(Morse) 【内附脑图】
参加 2018 ngChina 开发者大会,特别喜欢 Michael Hladky 奥地利帅哥的 RxJS 分享,现在拿出来好好学习工作坊的内容(工作坊Demo地址),结合这个示例,做了一个改进版本, ...
- rxjs 常用的管道操作符
操作符文档 api 列表 do -> tap catch -> catchError switch -> switchAll finally -> finalize map s ...
- RxJS v6 学习指南
为什么要使用 RxJS RxJS 是一套处理异步编程的 API,那么我将从异步讲起. 前端编程中的异步有:事件(event).AJAX.动画(animation).定时器(timer). 异步常见的问 ...
- Angular快速学习笔记(4) -- Observable与RxJS
介绍RxJS前,先介绍Observable 可观察对象(Observable) 可观察对象支持在应用中的发布者和订阅者之间传递消息. 可观察对象可以发送多个任意类型的值 -- 字面量.消息.事件. 基 ...
随机推荐
- 异常+远程控制Linux-14
什么是异常 a=8950/0 ZeroDivisioonError: division by zero print (a) ************** b = [1,2] ...
- 机器学习进阶-图像金字塔与轮廓检测-模板匹配(单目标匹配和多目标匹配)1.cv2.matchTemplate(进行模板匹配) 2.cv2.minMaxLoc(找出矩阵最大值和最小值的位置(x,y)) 3.cv2.rectangle(在图像上画矩形)
1. cv2.matchTemplate(src, template, method) # 用于进行模板匹配 参数说明: src目标图像, template模板,method使用什么指标做模板的匹配 ...
- Redis简单生产者消费者
注意:redis客户端执行是单线程的,不能将客户端放在外面,内部执行使用多线程的方式. // 创建生产端连接 final Jedis jedisProducter = new Jedis(R_HOST ...
- jquery查找筛选器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- The value for the useBean class attribute xxx is invalid
JSP页面报这个错可能的原因: 1:指定的 Bean 类没找到 2:该类不是 public 的,或者找到的 class 文件是 interface 或抽象类 3:Bean 类中没有 public 的无 ...
- weka连接mysql数据库
一.下载并解压数据库驱动 下载地址:http://www.mysql.com/products/connector/,本文下载 mysql-connector-java-5.0.8.zip.将解压后的 ...
- linux 3.10 gro的理解和改进
gro,将同一个flow的一定时间范围之内的skb进行合并,减少协议栈的消耗,用于收包性能提升.gro网上的资料很多,但是都很少谈到gro的改进,刚好身边有个同事也想改这块的内容, 所以将最近看的gr ...
- Haskell语言学习笔记(73)Existentials
Existentials(存在类型) Existentially quantified types(Existentially types,Existentials)是一种将一组类型归为一个类型的方式 ...
- 新版openvpn for pc使用旧证书问题的处理
在client.ovpn中增加一句: tls-cipher "DEFAULT:@SECLEVEL=0"
- js实现刷新页面出现随机背景图
直接上代码: <script> var bodyBgs = []; bodyBgs[0] = "IMG/01.jpg"; ...