【发布时间】:2019-10-27 04:29:24
【问题描述】:
我有两个要尝试完成的异步请求,第二个基于第一个的结果。我尝试这样做的方式是:
- 监听第一个动作是否成功:
actions.GetAllItems - 根据ID从商店中选择相关商品:
this.store.select(selectors.getItemsById) - 映射返回的 ID,以便我可以对第一次调用返回的 ID 数组中的每个项目进行第二次调用
- 将结果放入 redux 存储中,渲染以查看。
我现在的方式确实成功地将它放入了我的 redux 商店。但是,由于它只是香草Array.map,因此它不会返回可观察值。这意味着 observable 没有存储在this.details$ 中,这意味着它不会在我的模板中使用{{ details$ | async | json }} 呈现
如何根据第一次 XHR 调用的结果实现第二次 XHR 调用?
ngOnInit() {
this.store.dispatch(new actions.GetAllItems())
this.details$ = this.actions$.pipe(
ofType(actions.types.GetAllItemsSuccess),
mergeMap(() => {
return this.store.select(selectors.getItemsById); // filter to multiple items based on item ID
}),
map((items: models.IItemGeneralResponse[]) => {
items.map(item => { // sync map does not seem like it belongs in rxjs
this.store.dispatch(
new actions.GetItemDetail(item.id)
);
});
})
);
}
【问题讨论】:
-
你能分享一下 details$ observable 的类型吗?是否可观察
?
标签: javascript angular rxjs observable ngrx