对于后台传递到前台的数据,接收后,如果想要向数据对象新增属性,普通的新增属性,虽然能够使对象新增属性,但是属性值是不会跟随页面上的视图更新的
data () {
return {
goods: {
id: '',
name: ''
}
}
如果直接增加属性的话
add() {
this.goods.img = 'xx.jpg'
}
此时,goods 对象的属性值已经更新了,但是 vue 视图并没有任何变化,img 的值在页面视图上仍然是空的。
vue 中如果想要给对象新增属性,并触发视图更新
解决办法: 使用 this.$set(this.data, 'key', value)
add() {
this.$set(this.goods, 'img', 'xx.jpg')
}
注意:如果 vue 项目中使用了 keep-alive,那么页面存在缓存,在编辑不同项时页面数据未更新的解决办法,使用 activated
代替created
,页面只有第一次刷新使用 created
,后面都使用 activated
activated() {
getRaceType(this)
getSubrace(this, this.$route.query.raceid, this.$route.query.subraceid)
getlastrace(this, this.$route.query.raceid, this.$route.query.subraceid)
},