简体   繁体   English

Vue.js 组件不会对数据更新做出反应

[英]Vue.js component doesn't react on data updates

Try to render a data into <table></table> and add a event handler to update the data.尝试将数据呈现到<table></table>并添加事件处理程序以更新数据。 My initial data is array of arrays.我的初始数据是数组数组。 And handler just update these arrays.处理程序只是更新这些数组。 Table rendered, handler forks fine and updates arrays, but updates doesn't show on the <table></table> .表渲染,处理程序叉很好并更新数组,但更新不会显示在<table></table>上。 Line {{ grid[row][column] }} doesn't show updated data.{{ grid[row][column] }}不显示更新的数据。 Where is my mistake?我的错误在哪里?

Vue.component('grid-component', {
  template: `
  <table>
    <tbody>
      <tr v-for="(row, rowIndex) in grid">
        <td v-for="(column, columnIndex) in row" @click="onClickHandler" v-bind:row="rowIndex" v-bind:column="columnIndex">
          {{ grid[row][column] }}
        </td>
      </tr>
    </tbody>
  </table>
  `,
  props: ['grid', 'onClickHandler']
})

var app = new Vue({
  el: '#app',
  data: {
    grid: createGrid(columnsInGrid, rowsInGrid),
  },
  methods: {
    put(event) {
      const target = event.target;
      const column = target.getAttribute('column');
      const row = target.getAttribute('row');
      this.grid[row][column] = 1;
    }
  }
})

In index.html:在 index.html 中:

<div id="app">
    <div is="grid-component" v-bind:grid="grid" v-bind:on-click-handler="put"></div>
</div>

It doesn't work by design of vue reactivity.它不适用于 vue 反应性的设计。 Please read https://v2.vuejs.org/v2/guide/list.html#Caveats .请阅读https://v2.vuejs.org/v2/guide/list.html#Caveats There is no technical way to detect changes like grid[index] .没有技术方法可以检测像grid[index]这样的变化。

Change squared braces the Vue setter like:更改 Vue 设置器的方括号,例如:

Vue.set(grid[row], column, 1)

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

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