throw
优质
小牛编辑
135浏览
2023-12-01
throw
函数签名: throw(error: any, scheduler: Scheduler): Observable
在订阅上发出错误
示例
示例 1: 在订阅上抛出错误
( StackBlitz | jsBin | jsFiddle )
// RxJS v6+
import { throwError } from 'rxjs';
// 在订阅上使用指定值来发出错误
const source = throwError('This is an error!');
// 输出: 'Error: This is an error!'
const subscribe = source.subscribe({
next: val => console.log(val),
complete: () => console.log('Complete!'),
error: val => console.log(`Error: ${val}`)
});
相关示例
其他资源
其他资源
- throw :newspaper: - 官方文档
- 创建操作符: empty, never 和 throw :video_camera: :dollar: - André Staltz
源码: https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/throwError.ts