【发布时间】:2019-08-19 07:27:16
【问题描述】:
我正在尝试订阅一个主题,该主题将在 forEach 中重复发送多次。但是我只订阅了一次,实际上应该是 7 次。
我试图延迟一些时间来使用 setTimeOut 执行“next()”方法。但它不起作用。 角 Cli:6.2.9 节点:10.15.3 操作系统:win32 x64 rxjs:6.2.2 打字稿:2.7.2
private selectedVersion$: Subject<LeadTimeVersion> = new Subject<LeadTimeVersion>();
productLineId: string;
nodes: LeadTimeNode[] =[];
filterNodes: LeadTimeNode[];
versions: LeadTimeVersion[];
constructor( private leadTimeService: LeadTimeService) { }
ngOnInit() {
...
let counter = 0;
this.selectedVersion$.pipe(
switchMap(v => this.leadTimeService.getLeadTimeVersionData(this.productLineId, v.id)),
takeUntil(this.unsubscribe)
).subscribe(x => {
this.nodes = mergeArray(this.nodes, x.nodes);
counter++;
if(counter === this.versions.length){
this.filterTables('');
}
});
this.leadTimeService.selectedLeadTimeWorkbench$.pipe(
takeUntil(this.unsubscribe)
).subscribe(x => {
this.productLineId = x.id;
this.versions = x.workingVersions;
this.versions.forEach(v => {
this.selectedVersion$.next(v);
})
});
}
【问题讨论】:
-
一次性发出数组对象可能更好
-
将
switchMap更改为mergeMap。switchMap正在取消您之前的 API 响应并仅发出 API 的最新值。 -
@user2216584,谢谢,它有效
-
如果上面的答案正在解决您的问题,那么您应该将其发布为答案。
标签: angular rxjs observable subject