我正在尝试创建一种效果,通过它从商店(this.libraryReferencesStoreFacade.selectSelectedRows$)
循环收集信息,并最终对另一个动作(LibraryReferencesActions.patch({pathParameters: { id: 120},request: { id: row.id, group: 26 }})
进行多次调用。
我尝试了下面的代码,但没有成功,请帮助我?
@Effect()
someEffect$: Observable<Action> = this.actions$.pipe(
ofType(LibraryReferencesActions.createGroupSuccess),
withLatestFrom(this.libraryReferencesStoreFacade.selectSelectedRows$),
switchMap(([, rows]) => {
console.log(rows,"")
let Observables: Array<Observable<any>> = [];
rows.forEach((row) => {
Observables.push(
LibraryReferencesActions.patch({
pathParameters: { id: 120 },
request: { id: row.id, group: 26 },
})
);
});
const observables: Array<Observable<any>> = Observables;
return forkJoin(observables).map(result => new
LibraryReferencesActions.refreshList())
}
})
)
也尝试:
addGroupToLib$ = createEffect(() => {
return this.actions$.pipe(
ofType(LibraryReferencesActions.createGroupSuccess),
withLatestFrom(this.libraryReferencesStoreFacade.selectSelectedRows$),
map(([, rows]) => {
let Observables: any = [];
rows.forEach((row) => {
Observables.push(
LibraryReferencesActions.patch({
pathParameters: { id: 120 },
request: { id: 120, group: 26 },
})
);
});
forkJoin(Observables).pipe(
map(() => {
return LibraryReferencesActions.refreshList();
})
);
})
);
});
我认为您可以将refreshList
动作添加到patch
动作数组的末尾,它们应该按顺序运行:
addGroupToLib$ = createEffect(() => {
return this.actions$.pipe(
ofType(LibraryReferencesActions.createGroupSuccess),
withLatestFrom(this.libraryReferencesStoreFacade.selectSelectedRows$),
switchMap(([, rows]) => {
return rows
.map(row => {
return LibraryReferencesActions.patch({
pathParameters: { id: 120 },
request: { id: row.id, group: 26 }
})
})
.concat(
LibraryReferencesActions.refreshList()
)
})
);
});