如何在Angular 8中使用Ngrx reducer更新state属性的一部分



这是我的初始状态。它只是添加了名称,而不是在car类下作为正确的对象

export const initialState: CustomerState = {
customer: new Customer(),
};
export class Customer{
id: number;
age: number;
cars: carClass;
phoneNumbers: string[];
}
export class carClass{
name:string;
citiesRegistered:string[];
}

这是我的减速器。我正在尝试更新状态,但我不确定如何更新类的嵌套组件

export const _customerReducer = createReducer(
initialState,
on(addNameToCustomerCarRequest , (state, action) => ({
...state,
customer: { ...state.customer, cars: action.name} 

})),
);

下面是更新汽车类

名称的操作
export const addNameToCustomerCarRequest = createAction(
'[Customer Component] Add Name to customer car request ',
props<{ name: string }>()
);

试试这个:

export const _customerReducer = createReducer(
initialState,
on(addNameToCustomerCarRequest , (state, action) => ({
...state,
customer: { ...state.customer, cars: { ...state.cars, name: action.name } } 

})),
);
This is the new syntax and I am not familiar with it. It should either be `action.payload.name` or `action.name`.

最新更新