首页 > 解决方案 > NGRX - 在单个效果的多个动作中捕捉动作

问题描述

我有一个效果监听多个动作并执行一些代码,每个动作可能以不同的顺序分派,我需要捕捉第一个和最后一个动作何时被分派并在映射发生之前执行一些逻辑,这是示例代码:

@Effect({dispatch: false})
    updated$ = this.actions$.pipe(
        ofType(
            actionsType.ACTION1,
            actionsType.ACTION2,
            actionsType.ACTION3,
            actionsType.ACTION4,
        ),
        withLatestFrom(this.store$.pipe(
            select((state) => state)
        )),
        map(([action, state]) => {
            let objectMapped: {};
            objectMapped = buildSomething(state, action.type);
            return objectMapped;
        }),
        tap( (result: any) => {
          // I need to know here when the first and the last action happened to do some extra logic.
        })
    );

标签: javascriptangularreduxngrx

解决方案


type您可以使用 , 的属性检查当前执行的有效操作action,以执行某些特定代码。

map(action => {
  if (action.type === actionsType.ACTION1) {
    ...
  }
})

推荐阅读