【问题标题】:NGRX effect does not see the full state as it is in the storeNGRX 效果看不到商店中的完整状态
【发布时间】:2022-01-09 04:09:10
【问题描述】:

我有一个表单,它分为几个子表单,每个子表单都有自己的提交按钮,用于分派一个动作,调用我的减速器。 我店里的formdata是这样的(例子)

myData {
   propA1?: string,
   propA2?: string,
   propB1?: string,
   propB1?: string,
}

我的减速器看起来像这样

const myDataReducer = createReducer(
  initialState,
  on(FormActions.setData, (state, action) => ({
    ...state,
    ...action.data,
  }))
);

两种形式都派发一个动作

this.dispatch(FormActions.setData({ data}));

在我的 devtools 中,我看到了商店中的数据。到目前为止,一切都很好。 接下来我要做的是创建一个效果,它也监听 FormActions.setData,这样我就可以在 formdata 完成时调用一个 api。我创造了这个效果:

readonly getCalculation$ = createEffect(() =>
    this.actions$.pipe(
      ofType(FormActions.setData),
      withLatestFrom(this.store.select(formSelector.getData)),
      switchMap(([{ data}]) => {
        console.log(data);  // this logs only partial data
        return this.service.getCalculation(data).pipe(
          map((response) => calculationActions.getCalculationSuccess({ response: response })),
          catchError((error) => {
            console.warn('error in getcalculation', error);
            return EMPTY;
          })
        );
      })
    )
  );

我想要实现的是这个效果用完整的数据调用我的计算服务。但是当提交子表单A时,效果中的数据只有'A'属性,子表单B也是如此。

我希望 .withLatestFrom(this.store...) 我会得到完整的数据。我在我的 devtools 中看到了商店中的完整数据,但不知何故我的效果没有看到所有属性。

我将 Angular9 与 NgRx 9 一起使用

【问题讨论】:

    标签: angular ngrx ngrx-store ngrx-effects


    【解决方案1】:

    这是因为myData 是操作中存在的数据。 您必须选择数组中的第二项,这是选择器的结果。

    ofType(FormActions.setData),
    withLatestFrom(this.store.select(formSelector.getData)),
    //         ? skip the action
    //             ? pluck the data from selector result
    switchMap(([_, data]) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-21
      • 2018-02-16
      • 2021-09-08
      • 2019-09-30
      • 1970-01-01
      • 2020-01-16
      • 2020-11-16
      相关资源
      最近更新 更多