首页 > 解决方案 > Observable 受变量控制

问题描述

我尝试让我的 HTTP 客户端请求由变量控制。但我不确定如何设置过滤器。

任何帮助,将不胜感激。

go : boolean = true;     // skip the request if false


ngOnInit ()
{
  interval (10000).pipe (
    startWith (0),
    mergeMap (obs => this.myservice.request ().pipe (catchError (error =>
    {
      console.log ("error: " + error);
      return empty ();
    })))).subscribe (resp =>
    {
      console.log ("response: " + resp);
    });
}

标签: angularrxjsangular-httpclient

解决方案


margeMap可以根据go变量返回不同的 Observable:

mergeMap (obs => go ? this.myservice.request().pipe(...) : EMPTY)

哪里EMPTY可以从'rxjs'包中导入。


推荐阅读