ignoreElements

签名: ignoreElements(): Observable

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

ignoreElements - 图1

示例

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

( jsBin |
jsFiddle )

  1. import { interval } from 'rxjs/observable/interval';
  2. import { take, ignoreElements } from 'rxjs/operators';
  3. // 每100毫秒发出值
  4. const source = interval(100);
  5. // 忽略所有值,只发出 complete
  6. const example = source.pipe(take(5), ignoreElements());
  7. // 输出: "COMPLETE!"
  8. const subscribe = example.subscribe(
  9. val => console.log(`NEXT: ${val}`),
  10. val => console.log(`ERROR: ${val}`),
  11. () => console.log('COMPLETE!')
  12. );
示例 2: 只显示错误

( jsBin |
jsFiddle )

  1. import { interval } from 'rxjs/observable/interval';
  2. import { _throw } from 'rxjs/observable/throw';
  3. import { of } from 'rxjs/observable/of';
  4. import { mergeMap, ignoreElements } from 'rxjs/operators';
  5. // 每100毫秒发出值
  6. const source = interval(100);
  7. // 忽略所有值,只发出 error
  8. const error = source.pipe(
  9. mergeMap(val => {
  10. if (val === 4) {
  11. return _throw(`ERROR AT ${val}`);
  12. }
  13. return of(val);
  14. }),
  15. ignoreElements()
  16. );
  17. // 输出: "ERROR: ERROR AT 4"
  18. const subscribe = error.subscribe(
  19. val => console.log(`NEXT: ${val}`),
  20. val => console.log(`ERROR: ${val}`),
  21. () => console.log('SECOND COMPLETE!')
  22. );

其他资源


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