0

我尝试让我的 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);
    });
}
4

2 回答 2

2

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

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

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

于 2020-12-21T14:28:10.323 回答
0

你也可以使用 || 操作员

 mergeMap (obs => this.myservice.request().pipe(...) || null)
于 2020-12-21T14:30:37.460 回答