NGRX效果-映射操作以进行顺序服务调用和更新存储 [英] NGRX effects - mapping actions to make sequential service calls and updating store

查看:80
本文介绍了NGRX效果-映射操作以进行顺序服务调用和更新存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想连续2次将相同的动作传递给不同的实体,例如,我想添加2个实体,

I want to pass the same action 2 successive times with different entities, for example i want to add 2 entities,

entity 1;
entity 2;
this.store.dispatch(new Add(entity1));
this.store.dispatch(new Add(entity2));

我遇到的问题是我只有一个动作(实体2一个)的结果 这就是我行动的结果.我想等待第一个动作的结果再通过第二个动作.

the problem that i encountered is that i got the result of only one action (the entity 2 one) and this is the effect of my action. I want to wait for the result of the first action before passing the second action.

@effect()
add$ = this.actions$
.ofType<Add>(ADD)
.pipe(switchMap(a =>
this.sharedService.add(a.entity, a.api)
.pipe(map(s => new AddResult(Ok(s.id))))
.pipe(catchError(e => of(new AddResult(Err(e)))))));

推荐答案

您可能要使用 mergeMap 而不是 switchMap .以下是具有其行为的地图列表:

You may want to use mergeMap instead of switchMap. Here is list of Map with their behaviour:

@effect()
add$ = this.actions$
.ofType<Add>(ADD)
.pipe(mergeMap(a =>
this.sharedService.add(a.entity, a.api)
.pipe(map(s => new AddResult(Ok(s.id))))
.pipe(catchError(e => of(new AddResult(Err(e)))))));

这篇关于NGRX效果-映射操作以进行顺序服务调用和更新存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆