由变量控制的可观察的



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

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

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

其中CCD_ 3可以从CCD_。

您还可以使用||运算符

mergeMap (obs => this.myservice.request().pipe(...) || null)

最新更新