of

签名: of(...values, scheduler: Scheduler): Observable

按顺序发出任意数量的值。

示例

示例 1: 发出数字序列

( jsBin |
jsFiddle )

  1. // 依次发出提供的任意数量的值
  2. const source = Rx.Observable.of(1, 2, 3, 4, 5);
  3. // 输出: 1,2,3,4,5
  4. const subscribe = source.subscribe(val => console.log(val));
示例 2: 发出对象、数组和函数

( jsBin |
jsFiddle )

  1. import { of } from 'rxjs/observable/of';
  2. // 发出任意类型值
  3. const source = of({ name: 'Brian' }, [1, 2, 3], function hello() {
  4. return 'Hello';
  5. });
  6. // 输出: {name: 'Brian}, [1,2,3], function hello() { return 'Hello' }
  7. const subscribe = source.subscribe(val => console.log(val));

其他资源

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