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!')
);

其他资源


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

results matching ""

    No results matching ""