angular - 如何从ngrx效果多次调度相同的 Action

标签 angular ngrx-store ngrx-effects

我想从我的效果中多次分派(dispatch)一个 Action ,为此我使用 concatMap ,但是当我发送相同的操作时,它会在下一次发送时被取消。有没有办法在之前的分派(dispatch)完成时分派(dispatch)一个 Action 。

下面的代码将有助于更好地理解问题。

@Effect()
  setCategory$ = this.actions$
    .ofType(GET_SESSION_AD_SUCCESS)
    .concatMap((action: Action) => {

      const category = action.payload;
      const categoryPath = category.path;
      const categoryDispatchArray = [];

      categoryPath.forEach((path, index) => {
        categoryDispatchArray.push({
          type: GET_SUBCATEGORIES_BY_PARENT,
          payload: { categoryId: path.id, index }
        });
      })

      return dispatchArray;

}

最佳答案

我认为您的问题是 concatMapped Observable 在传递给它的最后一个 Observable 关闭时关闭。您需要的是mergeMap (又名 flatMap )在这里。

我也会使用 .map创建 dispatchArray:

@Effect()
  setCategory$ = this.actions$
    .ofType(GET_SESSION_AD_SUCCESS)
    .mergeMap((action: Action) => {

      const category = action.payload;
      const categoryPath = category.path;

      const categoryDispatchArray = categoryPath
        .map((path, index) => ({
          type: GET_SUBCATEGORIES_BY_PARENT,
          payload: { categoryId: path.id, index }
        });

      return categoryDispatchArray;
    }

关于angular - 如何从ngrx效果多次调度相同的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45587533/

相关文章:

angular - NGRX Redux 存储——仅选择状态的一部分返回 'undefined',即使智能感知自动完成并且模型中的所有内容都匹配

angular - 如何在 ag-grid 中加载数据时避免 "no rows"消息

angular6 - 如何在ngrx/effects中将参数(action.payload)传递给服务?

Angular NgRx 效果,如何传递参数?

javascript - 在 ajax 调用和 html 呈现之后 Angular 访问数据

html - 组内可拖放的多个列表

javascript - ngrx-默认值被传播运算符覆盖了吗?

redux - 我应该把用于转换数据的业务逻辑放在哪里 ngrx 存储 : into effects or reducers?

node.js - ionic - 关于版本的困惑

angular - Angular 4 上的动画似乎没有过渡效果