NGRX angular 有效载荷或无有效载荷
NGRX angular payload or no payload
在 NGRX 库 (https://ngrx.io/) 中,当创建包含数据的操作时,示例显示提供的 payload
参数来包含此数据。有没有理由我不能只提供有效负载作为参数?我能找到的文档和所有示例都使用 payload
但没有真正解释原因。例如:
export class CreateEmployeeScheduleError implements Action {
readonly type = CREATE_EMPLOYEE_SCHEDULE_ERROR;
constructor(public payload: {
error: string,
requests: ScheduleCreateRequest[],
requestsRemaining: number
}) {}
}
可以写成:
export class CreateEmployeeScheduleError implements Action {
readonly type = CREATE_EMPLOYEE_SCHEDULE_ERROR;
constructor(public error: string,
public requests: ScheduleCreateRequest[],
public requestsRemaining: number) {}
}
这消除了获取 action.payload 的需要,以及对辅助函数 (map(toPayload)
) 的需要,并且类似于我看到的其他 redux 实现框架 (NGXS) .我有什么理由不应该像后者那样做吗?
这也会使 effects 和 reducers 变得更简单。
您不必使用 payload
这只是一个约定。
请参阅 让我们聊聊 NgRx 中的 Actions 和 Action Creators
来自ngrx.io
The interface has a single property, the type
, represented as a string. The type
property is for describing the action that will be dispatched in your application. The value of the type comes in the form of [Source] Event
and is used to provide a context of what category of action it is, and where an action was dispatched from. You add properties to an action to provide additional context or metadata for an action. The most common property is the payload
, which adds any associated data needed for the action.
在 NGRX 库 (https://ngrx.io/) 中,当创建包含数据的操作时,示例显示提供的 payload
参数来包含此数据。有没有理由我不能只提供有效负载作为参数?我能找到的文档和所有示例都使用 payload
但没有真正解释原因。例如:
export class CreateEmployeeScheduleError implements Action {
readonly type = CREATE_EMPLOYEE_SCHEDULE_ERROR;
constructor(public payload: {
error: string,
requests: ScheduleCreateRequest[],
requestsRemaining: number
}) {}
}
可以写成:
export class CreateEmployeeScheduleError implements Action {
readonly type = CREATE_EMPLOYEE_SCHEDULE_ERROR;
constructor(public error: string,
public requests: ScheduleCreateRequest[],
public requestsRemaining: number) {}
}
这消除了获取 action.payload 的需要,以及对辅助函数 (map(toPayload)
) 的需要,并且类似于我看到的其他 redux 实现框架 (NGXS) .我有什么理由不应该像后者那样做吗?
这也会使 effects 和 reducers 变得更简单。
您不必使用 payload
这只是一个约定。
请参阅 让我们聊聊 NgRx 中的 Actions 和 Action Creators
来自ngrx.io
The interface has a single property, the
type
, represented as a string. Thetype
property is for describing the action that will be dispatched in your application. The value of the type comes in the form of[Source] Event
and is used to provide a context of what category of action it is, and where an action was dispatched from. You add properties to an action to provide additional context or metadata for an action. The most common property is thepayload
, which adds any associated data needed for the action.