zishu's blog

zishu's blog

一个热爱生活的博主。https://zishu.me

Add data to Vue arrays using this.$set

Vue does not allow dynamically adding new root-level reactive properties to an already created instance. Instead, you can use the Vue.set(object, key, value) method to add reactive properties to nested objects.

If you directly add objects to an array, the view will not update, but the correct values can be output. Only by using this.$set to override can the view be updated.

 for (let i = 0; i < this.data.length; i++) {
  // this.data[i].state = false;
  this.$set(this.data[i], 'state', false)
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.