single

签名: single(a: Function): Observable

发出通过表达式的单一项。

single - 图1

示例

示例 1: 发出通过断言的第一个数字

( jsBin |
jsFiddle )

  1. import { from } from 'rxjs/observable/from';
  2. import { single } 'rxjs/operators';
  3. // 发出 (1,2,3,4,5)
  4. const source = from([1, 2, 3, 4, 5]);
  5. // 发出匹配断言函数的一项
  6. const example = source.pipe(single(val => val === 4));
  7. // 输出: 4
  8. const subscribe = example.subscribe(val => console.log(val));

其他资源


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