简体   繁体   English

Vue 对象更改,但不会重新渲染

[英]Vue object changes, but doesn't re-render

When I press right arrow key, it changes the object, but not re-render it :当我按右箭头键时,它会更改对象,但不会重新渲染它:

<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>

By key pressed(@keyup.arrow-keys="show") want to change the active cell.通过按键(@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);
    },

it worked nice with the static object:它适用于静态对象:

 mapMatrix: {
        0 : {
            0 : {
                  "view" : "-1",
                  "available" : true,
                  "active": false
            },
            1 : {
                  "view" : "1",
                  "available" : true,
                  "active": false
            },
            2 : {
              "view" : "1",
              "available" : true,
              "active": false
            },

          },
...
}

But doesn't work with:但不适用于:

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
              };
        }
      }
    }

It changes the object properly, but no reaction on html render.它正确地更改了对象,但对 html 渲染没有反应。 What is the difference?有什么区别?

The way you're building the matrix object won't work correctly with Vue;您构建矩阵对象的方式无法在 Vue 中正常工作; it likely won't end up being reactive (see Change Detection Caveats ).它可能最终不会是被动的(请参阅Change Detection Caveats )。

Either use this.$set when building the matrix, or build it first in a local variable then assign it to this.mapMatrix last to ensure that the whole object will be reactive.在构建矩阵时使用this.$set ,或者先在局部变量中构建它,然后最后将其分配给this.mapMatrix以确保整个对象将是反应性的。

Something like this:像这样的东西:

fillMatrix() {
  // This is a fresh new unobserved object
  const mapMatrix = {};

  for (let i = 0; i <= CELL_NUMBER; i++) {
    mapMatrix[i] = {};

    for(let g = 0; g <= CELL_NUMBER; g++) {
      mapMatrix[i][g] = {
        view: this.setView(g),
        available: true,
        active: false,
      };
    }
  }
  
  // Since the this.mapMatrix property is reactive, Vue will detect this
  // assignment and make the new mapMatrix object reactive
  this.mapMatrix = mapMatrix;
}

Should change object-fill function to more reactive way (as in the @jcbdrn comment):应该将对象填充功能更改为更具反应性的方式(如@jcbdrn 评论):

fillMatrix: function(){
      var i;
      var g;
      for(i = 0; i <= CELL_NUMBER; i++){
          this.$set(this.mapMatrix,i,{});
          for(g = 0; g <= CELL_NUMBER; g++){
            var view = this.setVeiw(g);
             this.$set(this.mapMatrix[i],g,
               {
                   "view" : view,
                   "available" : true,
                   "active": false
             }
           );

        }
      }
    },

That solved the problem.这解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM