angular - 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);
});
}
解决方案
您margeMap
可以根据go
变量返回不同的 Observable:
mergeMap (obs => go ? this.myservice.request().pipe(...) : EMPTY)
哪里EMPTY
可以从'rxjs'
包中导入。
推荐阅读
- node.js - Github 操作 - 错误:进程已完成,退出代码
- javascript - 我应该如何在其他输入字段中获取 base64 格式的图像?
- docker - 如何使用 docker-compose 将当前主机目录挂载到 docker 容器中?
- mongodb - StartsWithIgnoreCase - Gremlin 图形数据库查询
- mysql - 多张图片 + Node.js + Sequelize + Mysql
- c# - 如何检测 HtmlAgilityPack 中的 dom 元素变化
- java - 在 Spring Boot 应用程序中获取 k8s 命名空间?
- python - 使用 python 日志记录模块,有没有办法指示程序运行了多少次?
- kubernetes - Kubernetes 部署 - 控制同时终止的 Pod 的最大数量
- javascript - 如何从 React 中的扩展类传递道具?