vue之数组变异

原文链接: http://www.cnblogs.com/stephenleee/p/10271881.html

1.vue不能监测出对象属性的添加与删除

可以用Vue.set(obj,key,value)

也可以用this.$set(this.someobj,key,value)来实现响应式

2.Object.assign添加新属性不会触发更新

// 代替 `Object.assign(this.someObject, { a: 1, b: 2 })`

this.someObject = Object.assign({}, this.someObject, { a: 1, b: 2 })

为声明的属性不可使用,最好提前声明为空值

3.数组

直接对数组设置某一索引的值或者更改数组长度  vue都无法监测

但是数组的变异方法vue都可以监测

像push,pop,shift,unshift,splice,sort,reverse都会改变原数组的  

concat filter siice不会改变原数组

也可以用Vue.set(vm.items,index,value)  同理this.$set

(data中属性指向其他this的时候,为非响应式)

转载于:https://www.cnblogs.com/stephenleee/p/10271881.html

猜你喜欢

转载自blog.csdn.net/weixin_30797199/article/details/94832330
今日推荐