VUE修改data中数组对象触发视图更新的几种方法

755 阅读1分钟

Vue 中, data 有一个数组对象,修改该数组元素时,不会触发视图更新,那么怎样触发视图更新呢?

- Vue.set

可以设置对象或数组的值,通过key或数组索引,可以触发视图更新 数组修改:

Vue.set(array, indexOfItem, newValue)

对象修改:

Vue.set(obj, keyOfItem, newValue)

- Vue.delete

删除对象或数组中元素,通过key或数组索引,可以触发视图更新

数组修改:

Vue.delete(array, indexOfItem)

对象修改:

Vue.delete(obj, keyOfItem)

- 数组对象直接修改属性可以触发视图更新

this.array[0].isShow= true; 
this.array.forEach(function(item){ item.isShow= true; });

- 数组赋值为新数组可以触发视图更新

this.array = this.array.filter(...) 
this.array = this.array.concat(...)

- Vue提供了数组的变异方法,可以触发视图更新**