Vue 2.0 中如果向数组中添加对象,但视图不更新,这是因为 Vue 数据绑定的原理是基于数组的变异方法(即:push、pop、shift、unshift、splice、sort、reverse)进行监测的。如果直接修改数组,Vue 就无法检测到更改并触发视图更新。
解决方法:使用变异方法或Vue.set()进行数组的更新操作。例如:
this.items.push({ message: 'new item' })
Vue 2.0 中如果向数组中添加对象,但视图不更新,这是因为 Vue 数据绑定的原理是基于数组的变异方法(即:push、pop、shift、unshift、splice、sort、reverse)进行监测的。如果直接修改数组,Vue 就无法检测到更改并触发视图更新。
解决方法:使用变异方法或Vue.set()进行数组的更新操作。例如:
this.items.push({ message: 'new item' })