RxJS empty(已弃用)

2020-10-13 18:38 更新

创建一个 Observable,该对象不向观察者发出任何项目,并立即发出完整的通知。

弃用说明

不赞成使用 EMPTY 常量,或 scheduled(例如scheduled([], scheduler)

  1. empty(scheduler?: SchedulerLike)

参量

调度器 可选的。默认值为 undefined。甲 SchedulerLike 使用用于调度完成通知的发射。

描述

只是发出“完成”信息,没有别的。

空的大理石图

此静态运算符对于创建仅发出完整通知的简单 Observable 很有用。它可用于与其他 Observable 组成,例如 mergeMap

例子

发出数字7,然后完成

  1. import { empty } from 'rxjs';
  2. import { startWith } from 'rxjs/operators';
  3. const result = empty().pipe(startWith(7));
  4. result.subscribe(x => console.log(x));

仅将奇数映射并展平到序列'a','b','c'

  1. import { empty, interval, of } from 'rxjs';
  2. import { mergeMap } from 'rxjs/operators';
  3. const interval$ = interval(1000);
  4. const result = interval$.pipe(
  5. mergeMap(x => x % 2 === 1 ? of('a', 'b', 'c') : empty()),
  6. );
  7. result.subscribe(x => console.log(x));
  8. // Results in the following to the console:
  9. // x is equal to the count on the interval eg(0,1,2,3,...)
  10. // x will occur every 1000ms
  11. // if x % 2 is equal to 1 print abc
  12. // if x % 2 is not equal to 1 nothing will be output

也可以看看

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号