[英]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.