针对vue2 uni-app经常莫名其妙地出现时而无法刷新、时而刷新不完全等问题的简要记录。
排除视图数据不小心没更新值的情况下,比如赋值对象错误(大小写、弄混啥的也不是干过),其它常见的频发场景:
- 表单提交后的数据整体没能及时刷新
- 子组件选择指定人员后,回到父组件时没能及时在页面刷新
- 多层数据嵌套
$set
this.$set(原对象,索引,替换的值)
this.$set(that.formData.ApprovalUser[that.listName], that.UserIndex, singleObj)
多层数据嵌套
vue2使用的是Object.defineProperty(),针对的是对象的某个属性,在vue的初始化阶段完成了对已有成员的监听,所以新增的属性无法监听。通过set方法就相当于初始化阶段的数据响应式处理。
set方法单独使用也不太行。感谢参考博文的有效解答,非常有用,在此留个小小的笔记。
const addItem = {
name: '附件' + (val.FileList.length + 1),
extname: '.' + data.FileName.split(".")[1],
url: http.config.imgBaseUrl + data.FileUrl,
};
val.FileList.push(addItem);
this.$nextTick(() => {
//添加变动的属性 给第一层的对象,至关重要
this.$set(
this.itemList,
Math.random(), Math.random()
);
this.itemList[idx1].NodeItem[idx2].FileList =
Object.assign([],
this.itemList[idx1].NodeItem[idx2]
.FileList, addItem)
});
$forceUpdate
this.$forceUpdate()