在我的Angular2应用程序中,我有一个用于在评论中添加重放的reducer:
Replays.reducer.ts:
case ADD_REPLAY:
let index_replay = state
.map(review => review.id)
.indexOf(action.payload.replayTo);
return [
...state.slice(0, index_replay),
Object.assign({}, state[index_replay], {
replays: [...state[index_replay].replays, action.payload]
}),
...state.slice(index + 1)
];
default:
return state;
}
当评论已经包含重放时,我可以添加重播没有问题,但是当注释数组中的重放数组为空时,我有错误:无法读取未定义的属性concat(可能是由于扩展运算符)
我该如何避免这种行为?
答案 0 :(得分:1)
问题是你在一个空数组(扩展运算符=== .concat)上调用.concat,因此你得到了未定义的错误。
试试这个:
const replays = state.index_replay ? : state.index_replay : [];
这会分配state.index_replay
或空数组进行重放。
接下来,在展开运算符中使用replays
数组。