如何在 Angular 8 中使用 Ngrx reducer 更新部分状态 属性
How to update part of the state property using Ngrx reducer in Angular 8
这是我的初始状态。它只是添加名称而不是在 car class 下作为正确的对象
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[];
}
这是我的减速器。我正在尝试更新状态,但不确定如何更新 class
的嵌套组件
export const _customerReducer = createReducer(
initialState,
on(addNameToCustomerCarRequest , (state, action) => ({
...state,
customer: { ...state.customer, cars: action.name}
})),
);
这是仅更新汽车名称的操作 class
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`.
这是我的初始状态。它只是添加名称而不是在 car class 下作为正确的对象
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[];
}
这是我的减速器。我正在尝试更新状态,但不确定如何更新 class
的嵌套组件export const _customerReducer = createReducer(
initialState,
on(addNameToCustomerCarRequest , (state, action) => ({
...state,
customer: { ...state.customer, cars: action.name}
})),
);
这是仅更新汽车名称的操作 class
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`.