我有一个包含数据数组的状态。某些操作完成后,我需要从该数组中删除所有数据。我使用Angular 2。
const intialState: State = {
someData: new Array<someData>()
};
export function stateReducer(state = intialState, action: StateActionList.StateActionList) {
switch (action.type) {
case StateActionList.REMOVE_DATA:
console.log(state.someData);
return {
...state,
someData: new Array<someData>()
}
}
}
在我的组件中,我有以下代码:
this.store.dispatch(new StateActions.RemoveData());
在国家行动中:
export const REMOVE_DATA="REMOVE_DATA";
export class RemoveData implements Action {
readonly type=REMOVE_DATA;
}
该代码不会为我返回从商店清理数组someData。该数组包含与之前相同的数据
答案 0 :(得分:0)
尝试显式返回空数组作为新状态:
case StateActionList.REMOVE_DATA:: {
return {
someData: []
};
}
答案 1 :(得分:-1)
这看起来有点可疑
someData: new Array<someData>()
someData 是变量名称和类型?
请参阅Declare an array in TypeScript
let arr1: boolean[] = [];
let arr2: boolean[] = new Array();
let arr3: boolean[] = Array();
let arr4: Array<boolean> = [];
let arr5: Array<boolean> = new Array();
let arr6: Array<boolean> = Array();
let arr7 = [] as boolean[];
let arr8 = new Array() as Array<boolean>;
let arr9 = Array() as boolean[];
let arr10 = <boolean[]> [];
let arr11 = <Array<boolean>> new Array();
let arr12 = <boolean[]> Array();
const state = {
someData: new Array()
};
console.log('empty', state.someData)
state.someData.push('a')
console.log('contains "a"', state.someData)
const remove = () => {
console.log('constians "a"', state.someData);
return {
...state,
someData: new Array()
}
}
const newState = remove()
console.log('empty', newState.someData)