【发布时间】:2019-09-22 17:05:09
【问题描述】:
当我按右箭头键时,它会更改对象,但不会重新渲染它:
<div class="map">
<div class="map-page" tabindex="0" @keyup.arrow-keys="show" ref="mapPage">
<template v-for="mapRow in mapMatrix">
<div v-for="cell in mapRow" @click="(cell.view === '1') ? showAlert() : false " v-bind:class="['map-cell',{'cell-active' : cell.active}]">
{{cell.view}}
</div>
</template>
</div>
<div>
通过按键(@keyup.arrow-keys="show") 想要更改活动单元格。
show: function (event) {
if(event.keyCode === 39){
if (this.selectedCellId !== CELL_NUMBER){
this.moveRight();
}
}
},
moveRight: function(){
this.$set(this.mapMatrix[this.selectedRowId][this.selectedCellId],'active',false);
this.selectedCellId++;
this.$set(this.mapMatrix[this.selectedRowId][this.selectedCellId],'active',true);
},
它与静态对象配合得很好:
mapMatrix: {
0 : {
0 : {
"view" : "-1",
"available" : true,
"active": false
},
1 : {
"view" : "1",
"available" : true,
"active": false
},
2 : {
"view" : "1",
"available" : true,
"active": false
},
},
...
}
但不适用于:
fillMatrix: function(){
var i;
var g;
for(i = 0; i <= CELL_NUMBER; i++){
this.mapMatrix[i] = {};
for(g = 0; g <= CELL_NUMBER; g++){
var view = this.setVeiw(g);
this.mapMatrix[i][g] =
{
"view" : view,
"available" : true,
"active": false
};
}
}
}
它会正确更改对象,但对 html 渲染没有反应。有什么区别?
【问题讨论】:
-
在 fillMatrix 函数中,尝试使用
this.$set,这将使数组上的那些键反应
标签: javascript vue.js components render