gpt4 book ai didi

angular - NGRX 8 将元素添加到存储中的数组

转载 作者:行者123 更新时间:2023-12-02 03:06:53 25 4
gpt4 key购买 nike

我正在使用 Angular ngrx 8。我在 redux 方面遇到了问题。

当我有以下界面时:

export interface PersonChecklist {
personName: string;
isChecked: boolean;
}

我想在商店中有人员数组。

行动:

export const savePersonToChecklist = createAction(
'[PERSON_API] Save person to checklist array',
props<{
personName: string;
}>()
);

reducer :

on(savePersonToChecklist, (state, action) => ({
...state,
jobStatus: {
...state.jobStatus,
personsReadyForChecklist:
[
{
...state.jobStatus.personsReadyForChecklist,
personName: action.personName,
isChecked: true
}
]

}
}))

我保存了每个调度操作的嵌套条目...我想要以下结构:

[
{personName: 'A',
isChecked: true},
{personName: 'B',
isChecked: true},
.
.
.]

最佳答案

为了使其正常工作,您需要修复一些问题:

on(savePersonToChecklist, (state, action) => ({
...state,
jobStatus: {
...state.jobStatus,
ordersReadyForChecklist: [
...state.jobStatus.ordersReadyForChecklist, // <-- This needs to be here
{
personName: action.personName, // <-- This needs to be "personName" not "workOrderNumber"
isChecked: true
}
]
}
}))

我已经在stackblitz上创建了一个工作示例,我希望我正确地猜出了您的要求...

希望这有帮助。

关于angular - NGRX 8 将元素添加到存储中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58764696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com