angular - 如何订阅 flatMap 的结果?
问题描述
如何订阅 flatMap 的结果?
timer(0, 2000)
.pipe(
flatMap(() => this.scannerService.scan(this.scanType)),
takeWhile(res => res.errorDesc !== "SUCCESS")
)
.subscribe((res: IScanResponse) => {
});
我试过在.subscribe()
之后使用pipe
。但我得到错误:
您在预期流的位置提供了无效对象。您可以提供 Observable、Promise、Array 或 Iterable。
我的远程功能是:
public scan(scanType: string): Observable<any> {}
我的进口是:
import { timer } from "rxjs";
import { takeWhile } from "rxjs/operators";
import { ScannerService } from "src/app/api/ScannerService";
import { flatMap } from "rxjs/operators";
解决方案
After the .pipe()
since .pipe()
returns an observable
const tm = timer(0, 2000).pipe(
flatMap(() => this.scannerService.scan(this.scanType)),
takeWhile(res => res)
).subscribe(res=> {
//your code
});;
推荐阅读
- c# - ASP.NET Core CRUD 工具:未找到但实际安装的命令
- javascript - 我可以在 chartjs 选项上使用三元,尤其是在图例上?
- javascript - 什么关键字用于通过双击网页上的单词来创建自定义工具提示?
- azure-digital-twins - Azure 数字孪生和租户
- python - 如何将我当前的 django 应用程序转换为 REST API?
- javascript - 强制重新加载使用 AJAX 刷新内容的整个页面
- python - 只接受一个范围内的输入
- unity3d - arkit3.0 无法统一添加引用对象库
- mysql - 带有 MySQL Enterprise 的 Galera 集群?
- java - CrudRepository 中的自定义 SQL 查询