【发布时间】:2019-10-10 08:08:47
【问题描述】:
据我了解,这是传播运算符的工作原理:
x=[1,2,3];
y=[...x,4,5];
// 这与 y=[1,2,3,4,5] 相同
const initialState={
ingredients: [
new Ingredient('Apples', 5),
new Ingredient('Tomatoes', 10),
]
};
export function shoppingListReducer( state=initialState, action:ShoppingListActions.ShoppingListActions ) {
switch(action.type) {
case ShoppingListActions.ADD_INGREDIENT:
return {
...state,
ingredients:[...state.ingredients,action.payload ]
}
default:
return state;
}
在上面的例子中是什么
return {
...state,
ingredients:[...state.ingredients,action.payload ]
}
评估为?
有效载荷的类型为成分:
export class Ingredient {
constructor(public name: string, public amount: number) {}
}
【问题讨论】:
-
你也可以传播对象:
{ ...state}这里state是一个对象。通过使用{},结果将是一个对象。 -
你的有效载荷类型是什么?
-
@RezaRahmati 有效载荷:成分
export class Ingredient { constructor(public name: string, public amount: number) {} } -
Ingredients与Ingredient不同。Ingredients是什么类型的?
标签: javascript angular typescript ecmascript-6 redux