从效果中分派多个动作:不同rxjs运算符之间的差异 [英] Dispatch multiple actions from effects: difference between different rxjs operators

查看:81
本文介绍了从效果中分派多个动作:不同rxjs运算符之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http请求成功后,我需要从ngrx效果中调度多个操作.

I need to dispatch multiple actions from an ngrx effect once an http request is successful.

似乎有几种方法行得通,有些则行不通,我不明白有什么区别.

There are several ways that seem to work and some that don't, I can't understand what the difference is.

@Effect()
    loadUser = this.actions
        .pipe(
            ofType(campaignActions.type.LOAD_CAMPAIGN),
            map((action: userActions.LoadUserAction) => action.payload),
            switchMap((payload: { id: number }) =>
                this.s.getUser(payload.id)
                    .pipe(
                        switchMap((data) => {

                            return from([
                                new userActions.LoadUserSuccessAction(),
                                new userActions.SomethingElseSuccessAction()
                            ]);
                        }),
                        catchError(() => of(new userActions.LoadUserFailureAction()))
                    )
            )
        );

首先,我可以使用switchMap或mergeMap.我相信不同之处在于,如果多次触发此效果,那么如果我使用switchMap,而没有使用mergeMap,则所有正在进行的请求都将被取消.

First of all, I can use switchMap or mergeMap. I believe the difference is that if this effect is triggered multiple times, any ongoing requests will be cancelled if I use switchMap and not if I use mergeMap.

在分派多个动作方面,以下所有工作:

In terms of dispatching multiple actions, all the following works:

  • 返回[action1,action2]
  • 从([action1,action2])返回
  • (动作1,动作2)的返回

以下内容无效:

  • (([action1,action2])的返回->效果"UserEffects.loadUser"调度了无效的动作:[对象对象],[对象对象]

前三个选项之间有什么区别? http请求后,mergeMap和switchMap怎么样?为什么最后一个选项不起作用?

What is the difference between all the first 3 options? What about mergeMap vs switchMap after the http request? Why doesn't the last option work?

推荐答案

这个问题可以分为几个部分:

This question can be separated into few sections:

+)mergeMap和switchMap之间的区别:

+) Difference between mergeMap and switchMap:

  • mergeMap = map + mergeAll.它将数据流映射到可观察流,每次内部可观察对象发出一个值,外部观察对象将收集所有发出的值并将其合并到新流中.

  • mergeMap = map + mergeAll. It maps a data stream to observable stream, and everytime the inner observable emits a value, the outer will collect and merge all emitted value into a new stream.

switchMap ,非常相似,但是正如您所知切换",即,如果下一个内部可观测对象发出新值,则前一个被取消(一次一个内部可观测对象).

switchMap, very similar, but as you can tell it "switches", i.e. if the next inner observable emits a new value, the previous one gets canceled (one inner observable at a time).

+)mergeMap和switchMap之间的相似之处:它们都可以将任何类似于流的对象转换为可观察对象.

+) Similarity between mergeMap and switchMap: They can both convert any stream-like object to an observable.

+)您的代码之间的区别:

+) Difference between your code:

  • return [action1,action2]确实确实使switchMap(()=> ...)类似于from([action1,action2]),即从数组中创建一个可观察对象.

  • return [action1, action2] does indeed make switchMap(() => ...) similar to from([action1, action2]), i.e. create an observable from an array.

从([action1,action2])返回,这是一个内部可观察到的东西,发出2个动作,而switchMap将这些动作合并回到其流中.

return from([action1, action2]), this is an inner observable that emits 2 actions, and switchMap merges those actions back to its stream.

(action1,action2)的返回= from([[action1,action2]))

return of(action1, action2) = from([action1, action2])

返回([action1,action2])会创建一个可观察的对象,该对象发出一个值,该值是一个数组,您不能调用store.dispatch(array_of_actions)

return of([action1, action2]), however, creates an observable that emits a single value, which is an array, and you cannot call store.dispatch(array_of_actions)

这篇关于从效果中分派多个动作:不同rxjs运算符之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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