数据丢失是框架的BUG,vue中的数据绑定是通过ES5中属性的特性实现的。所以没有设置特性的数据,就会丢失。以下mounted中的四种操作都会导致数据丢失。
{{ colors }}
{{ obj }}
{{ intro }}
解决方法:
第1,2种情况 使用新数组替换之前的老数组
this.colors = ["red", "pink", "blue","gold"]
第3种情况 使用新对象替换之前的老对象
this.obj = {siz: 200}
第4种情况 初始化这类数据即可
data() {
return {
colors: ["red", "green", "blue"],
obj: {},
intro: '' // 初始化info
};
},
除此之外,还可以使用vue提供的$set方法
this.$set(this.colors, 1, pink) // 修改数组的数据 this.$set(this.obj, 'size', 200) // 修改对象的数据



