这是Vuex商店中的一种变异方法。我可以执行已注释的代码并看到组件中的更改,但不能看到未注释的部分。
看起来只有当我用Array()再次初始化它时,状态才是反应性的。否则,我看不到组件中的更改,尽管我在最后一个console.log中看到的状态已经更改
move(state: GameState, vector: Array<number>) {
//const piece1: Piece = new Piece(7);
//state.boardState = Array(4).fill(null).map(x => Array(4).fill(null));
//state.boardState[0][0] = piece1;
const size = state.boardState.length;
const xComponent = vector[0];
const yComponent = vector[1];
for (let i = 0; i < size - 1; i++) {
const shifter = xComponent < 0 ? size - i - 1 : i;
const shifterPrev = xComponent < 0 ? size - i - 2 : i + 1;
for (let j = 0; j < size; j++) {
if (state.boardState[shifter][j] !== null) {
if (state.boardState[shifterPrev][j] === null) {
state.boardState[shifterPrev][j] = state.boardState[shifter][j];
state.boardState[shifter][j] = null;
} else if (state.boardState[shifterPrev][j].equals(state.boardState[shifter][j])) {
state.boardState[shifterPrev][j].increaseValue();
state.boardState[shifter][j] = null;
}
}
}
}
console.log(state.boardState);
}
发布于 2019-01-27 21:17:01
解决了。
https://vuejs.org/v2/guide/list.html#Array-Change-Detection
VueJS对突变没有反应,例如:
vm.items[indexOfItem] = newValue
它必须按照如下方式完成:
Vue.set(vm.items, indexOfItem, newValue)
发布于 2019-01-29 02:55:53
通常,在处理复杂的对象数组时,我会对数组执行splice
操作,并将其替换为更新后的数组。Vue可以看到变化。
https://stackoverflow.com/questions/54388390
复制相似问题