简体   繁体   English

如何从嵌套的 rxjs 运算符返回多个操作

[英]How can I return multiple actions from a nested rxjs operator

I'm having trouble dispatching multiple actions in the following effect:我无法在以下效果中分派多个操作:

    @Effect()
    someEffect$ = this.actions$.pipe(
      ofType(someAction: Actions),
      mergeMap(
        (action)=>{
          return this.authService.getProfile(action.payload.authInfo).pipe(
              map((response: any) => {

                  if (response.isErrorResponse) {
                    return new HeaderActions.AuthFail(response);
                  } else {

                 //here i'd like to return multiple actions
                    return [
                      new HeaderActions.FetchMPInfoSuccess(this.fetchMpInfoSuccesState(getMpProfileResponse)),
                      new HeaderActions.GetAccountStates(true)
                  ]
                  }
              }),
              catchError((error:HttpErrorResponse )=>{
                return of(new HeaderActions.AuthFail(response));
              })
            );
        }
      )
    );

I've tried adding an array of actions, in the return block, but that's giving an error that im not sending a valid action, any idea what i'm doing wrong?我尝试在返回块中添加一组动作,但这给出了一个错误,即我没有发送有效的动作,知道我做错了什么吗?

You can use switchMap instead of mergeMap .您可以使用switchMap而不是mergeMap Then use flatMap or switchMap instead of map .然后使用flatMapswitchMap代替map

The code should be like this:代码应该是这样的:

@Effect()
someEffect$ = this.actions$.pipe(
  ofType(someAction: Actions),
  switchMap(
    (action)=>{
      return this.authService.getProfile(action.payload.authInfo).pipe(
          flatMap((response: any) => {

              if (response.isErrorResponse) {
                return new HeaderActions.AuthFail(response);
              } else {

             //here i'd like to return multiple actions
                return [
                  new HeaderActions.FetchMPInfoSuccess(this.fetchMpInfoSuccesState(getMpProfileResponse)),
                  new HeaderActions.GetAccountStates(true)
              ]
              }
          }),
          catchError((error:HttpErrorResponse )=>{
            return of(new HeaderActions.AuthFail(response));
          })
        );
    }
  )
);

To access data in your child component you can use this approach with @Input() set stuff:要访问子组件中的数据,您可以将此方法与 @Input() 设置内容一起使用:

@Input() set(data: any) {
if (data) { console.log(data) }}

The data in html will be there always. html 中的数据将一直存在。

Map modifies each item emitted by a source Observable and emits the modified item. Map修改源 Observable 发出的每个项目并发出修改后的项目。

FlatMap , SwitchMap also applies a function on each emitted item but instead of returning the modified item, it returns the Observable itself which can emit data again. FlatMapSwitchMap还对每个发射的项目应用 function 但不是返回修改后的项目,而是返回可以再次发射数据的 Observable 本身。

FlatMap merge items emitted by multiple Observables and returns a single Observable. FlatMap合并多个 Observable 发出的项目并返回单个 Observable。

SwitchMap is a bit different from FlatMap. SwitchMap与 FlatMap 有点不同。 SwitchMap unsubscribes from the previous source Observable whenever new item started emitting, thus always emitting the items from current Observable.每当新项目开始发射时,SwitchMap 都会取消订阅前一个源 Observable,因此总是从当前 Observable 发射项目。

If you would like to know more about rxjs operators, please have a look here .如果您想了解更多关于 rxjs 运算符的信息,请查看此处

Try:尝试:


@Effect()
    someEffect$ = this.actions$.pipe(
      ofType(someAction: Actions),
      switchMap(
        (action)=>{
          return this.authService.getProfile(action.payload.authInfo).pipe(
              map((response: any) => {

                  if (response.isErrorResponse) {
                    return [new HeaderActions.AuthFail(response)];
                  } else {

                 //here i'd like to return multiple actions
                    return [
                      new HeaderActions.FetchMPInfoSuccess(this.fetchMpInfoSuccesState(getMpProfileResponse)),
                      new HeaderActions.GetAccountStates(true)
                  ]
                  }
              }),
              catchError((error:HttpErrorResponse )=>{
                return of(new HeaderActions.AuthFail(response));
              })
            );
        }
      )
    );

So basically What I did was, instead of using a mergeMap before the service call, I swapped it back to simple switchMap .所以基本上我所做的是,我没有在服务调用之前使用mergeMap ,而是将它换回了简单的switchMap

Reason why I changed it from mergeMap was: you are only returning value from a single stream.我从mergeMap更改它的原因是:您只从单个 stream 返回值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM