当前位置: 首页 > 文档资料 > 学习 RxJS 操作符 >

ignoreElements

优质
小牛编辑
134浏览
2023-12-01

ignoreElements

函数签名: ignoreElements(): Observable

忽略所有通知,除了 complete 和 error 。

示例

示例 1: 忽略源 observable 的所有数据项

( StackBlitz | jsBin | jsFiddle )

// RxJS v6+
import { interval } from 'rxjs';
import { take, ignoreElements } from 'rxjs/operators';

// 每100毫秒发出值
const source = interval(100);
// 略所有值,只发出 complete
const example = source.pipe(
  take(5),
  ignoreElements()
);
// 输出: "COMPLETE!"
const subscribe = example.subscribe(
  val => console.log(`NEXT: ${val}`),
  val => console.log(`ERROR: ${val}`),
  () => console.log('COMPLETE!')
);
示例 2: 只显示错误

( StackBlitz | jsBin | jsFiddle )

// RxJS v6+
import { interval, throwError, of } from 'rxjs';
import { mergeMap, ignoreElements } from 'rxjs/operators';

// 每100毫秒发出值
const source = interval(100);
// 忽略所有值,只发出 error
const error = source.pipe(
  mergeMap(val => {
    if (val === 4) {
      return throwError(`ERROR AT ${val}`);
    }
    return of(val);
  }),
  ignoreElements()
);
// 输出: "ERROR: ERROR AT 4"
const subscribe = error.subscribe(
  val => console.log(`NEXT: ${val}`),
  val => console.log(`ERROR: ${val}`),
  () => console.log('SECOND COMPLETE!')
);

其他资源


源码: https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/ignoreElements.ts