【发布时间】:2019-10-24 06:15:16
【问题描述】:
我想进入 VueJs 开发并创建了一个简单的扫雷游戏。二维网格由 Vuex 状态管理。单击单元格时,我想显示它,因此我当前的代码是
[MutationTypes.REVEAL_CELL]: (state, { rowIndex, columnIndex }) => {
state.board[rowIndex][columnIndex].isRevealed = true;
}
很遗憾,这对 UI 没有影响。这个问题是已知的并在此处描述
https://vuejs.org/v2/guide/list.html#Caveats
文档告诉我使用这样的东西
import Vue from "vue";
[MutationTypes.REVEAL_CELL]: (state, { rowIndex, columnIndex }) => {
const updatedCell = state.board[rowIndex][columnIndex];
updatedCell.isRevealed = true;
Vue.set(state.board[rowIndex], columnIndex, updatedCell);
Vue.set(state.board, rowIndex, state.board[rowIndex]);
}
但它没有帮助。最后,我尝试创建板的副本,修改值并将该副本分配给板。
[MutationTypes.REVEAL_CELL]: (state, { rowIndex, columnIndex }) => {
const newBoard = state.board.map((row, mapRowIndex) => {
return row.map((cell, cellIndex) => {
if (mapRowIndex === rowIndex && cellIndex === columnIndex) {
cell = { ...cell, isRevealed: true };
}
return cell;
});
});
state.board = newBoard;
}
这也不起作用。有人有想法吗?
我创建了一个 Codesandbox 来显示我的项目
https://codesandbox.io/s/vuetify-vuex-and-vuerouter-d4q2b
但我认为唯一相关的文件是 /store/gameBoard/mutations.js 和函数 REVEAL_CELL
【问题讨论】:
-
你能贴出你正在使用 Vuex 的组件代码吗?
-
我刚刚创建了一个沙盒 :)
-
它似乎网格没有对状态变化做出反应。不知道为什么 - 仍在搜索.. I did find something interesting though.. if you click a cell and then click back into the code, and press any key (space, for example) the cell will show itself... - 即使我通过 Vue 开发工具强制
commit它也没有显示单元格...非常有趣的问题...def似乎“板”更改没有进入必要的组件..为什么不只处理来自单元格组件的单元格点击而不是一直发出该事件像那样? -
把它放在这里是为了可见性 OP - 请将 Dan 的答案标记为正确 - 他是解决这个问题的人 - here is a working example - 全部我所做的就是复制并粘贴丹所说的话..
标签: javascript vue.js vuejs2 vuex computed-properties