由于 JavaScript 的限制, Vue 不能检测以下变动的数组:

  1. 当你利用索引直接设置一个项时,例如: vm.items[indexOfItem] = newValue
  2. 当你修改数组的长度时,例如: vm.items.length = newLength

为了解决第一类问题,以下两种方式都可以实现和 vm.items[indexOfItem] = newValue 相同的效果, 同时也将触发状态更新:

// Vue.set
Vue.set(example1.items, indexOfItem, newValue)
 
// Array.prototype.splice`
example1.items.splice(indexOfItem, 1, newValue)

为了解决第二类问题,你也同样可以使用 splice

example1.items.splice(newLength)

 

相关文章:

  • 2019-10-30
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-17
  • 2021-11-22
  • 2021-12-12
猜你喜欢
  • 2021-06-10
  • 2022-12-23
  • 2021-07-31
  • 2022-12-23
  • 2021-10-27
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案