简体   繁体   English

Angular NgRx - 当其他动作返回值时,从效果中分派一个动作

[英]Angular NgRx - dispatch an action from effect when other actions return a value

I'm having trouble dispatching actions that needs results from other five actions (that are listed in my effect).我无法调度需要其他五个操作(在我的效果中列出)的结果的操作。

Can somebody help me with this?有人可以帮我吗? To recap, I need to dispatch an action inside effect once these five actions have given back a value.回顾一下,一旦这五个动作返回一个值,我需要在效果内调度一个动作。

Here is the code after which I'd like to call getProject() :这是我想调用getProject()之后的代码:

    getProjectRefData$ = createEffect(() =>
    this.actions$.pipe(
      ofType(GetProjectRefData),
      mergeMap(() =>
        of(
          GetProjectSubTypes(),
          GetAccountCategories(),
          GetProductions(),
          GetCompetitions(),
          GetCompetitionEditions()
        )
      )
    )
  );

And here is the blueprint example of each of those five actions (or rather, effects they are triggering):这是这五个动作(或者更确切地说是它们触发的效果)中的每一个的蓝图示例:

    getCompetitionEditions$ = createEffect(() =>
    this.actions$.pipe(
      ofType(GetCompetitionEditions),
      mergeMap(() =>
        this._factoryService
          .getFactory('/referencedata/competitioneditions')
          .pipe(
            map((res: ICompetitionEdition[]) =>
              GetCompetitionEditionsSuccess({ payload: res })
            ),
            catchError(err => {
              console.error(err);
              return of(
                OpenSnackBar({
                  payload: {
                    type: 'error',
                    message: JSON.parse(err._body).message,
                    id: uuid.v4(),
                  },
                }),
                GetCompetitionEditionsError()
              );
            })
          )
      )
    )
  );

Go for forkJoin : Go forkJoin

   getProjectRefData$ = createEffect(() =>
    this.actions$.pipe(
      ofType(GetProjectRefData),
      switchMap(action =>
        forkJoin(
          GetProjectSubTypes(),
          GetAccountCategories(),
          GetProductions(),
          GetCompetitions(),
          GetCompetitionEditions()
        )
      ),
      map(([projectSubtypes, accountCategories, productions, competitions, competitionEndings]) => this.someService.get('pass args here'),
      map(callResult => SomeAction(callResult))
    )
  );

forkJoin will emit a combined value in a form of an array after all of inner observables complete.在所有内部可观察对象完成后, forkJoin将以数组的形式发出一个组合值。

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

相关问题 Angular Ngrx - 调度动作时不调用效果 - Angular Ngrx - Effects not be called when dispatch Actions NGRX-在单个效果中的多个动作中捕获动作 - NGRX - Catch action in multiple actions on single effect ngrx 8 影响其他类型的调度动作 - ngrx 8 effects dispatch action of other type 如何在 ngrx/effect (redux-observable) 中分派多个动作? - How to dispatch multiple actions in ngrx/effect (redux-observable)? 在 NgRx (Angular) 的效果中将动作链接在一起 - Chaining actions together in an effect for NgRx (Angular) @ ngrx / store调度从组件开始工作,但不适用于@ ngrx / effect - @ngrx/store dispatch working from component but not working from @ngrx/effect 在使用路由保护之前通过调度操作在ngrx效果中加载数据 - load data in ngrx effect with dispatch an action before using route guard Angular NGRX 在从效果中调度动作时无法将数据传递给减速器。 出现错误 - TypeError:无法冻结 - Angular NGRX cannot pass data to reducer when dispatching action from effect. Got Error - TypeError: Cannot freeze 使用 ofType 访问 @ngrx 效果中的动作道具,用于多个动作 - Access action props in @ngrx effect with ofType for multiple actions 如何在Angular NgRx效果中进行http调用之前调度动作? - How to dispatch an action before making an http call in Angular NgRx effects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM