如何在 Foreach 循环中多次重新发出主题?
How to re-emit a Subject several times in a Foreach loop?
我正在尝试订阅一个主题,该主题将在 forEach 中重新发出多次。但是我只订阅了一个,实际上是7次。
我试图延迟一些时间来执行带有setTimeOut 的"next()" 方法。但它不起作用。
Angular 客户端:6.2.9
节点:10.15.3
OS: 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 回答。谢谢。
我正在尝试订阅一个主题,该主题将在 forEach 中重新发出多次。但是我只订阅了一个,实际上是7次。
我试图延迟一些时间来执行带有setTimeOut 的"next()" 方法。但它不起作用。 Angular 客户端:6.2.9 节点:10.15.3 OS: 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 回答。谢谢。