【问题标题】:How to use an array as argument in createAction for @ngrx/store如何在 @ngrx/store 的 createAction 中使用数组作为参数
【发布时间】:2020-03-13 03:31:19
【问题描述】:

我想创建一个以数组为参数的动作,但这不进行类型检查:

export const action_organizations_available = createAction(
  CoreActionTypes.ORGANIZATIONS_AVAILABLE,
  props<Organization[]>()
);

我收到props(): "arrays are not allowed in action creators"。 当类型不是数组时,它可以正常工作。

使用props&lt;{ organizations: Organization[] }&gt; 有效,但我想知道这是否是最佳做法。

当期望数组作为动作中的参数时,最佳做法是什么?

【问题讨论】:

    标签: angular ngrx ngrx-store


    【解决方案1】:

    你必须把它包装成一个对象:

    export const action_organizations_available = createAction(
      CoreActionTypes.ORGANIZATIONS_AVAILABLE,
      props<{ organizations: Organization[] }>()
    );
    

    编辑:现在它是 NgRx ESLint 插件中的规则 "prefer-inline-action-props"

    【讨论】:

    • 谢谢。意识到无论如何总是将它包装到一个对象中实际上听起来更好。 ngrx doc 没有表现出这一点,但对我来说它看起来更干净。
    • 救了我的命。顺便说一句,在 createReducer 中,参数也需要包装在对象中,因为您在动作创建者中声明
    猜你喜欢
    • 2018-06-20
    • 2019-12-16
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 2017-11-29
    • 1970-01-01
    • 2017-08-30
    相关资源
    最近更新 更多