今天碰到了一個ue中對象屬性改變視圖不更新的問題,一開始還一直以爲是vue中@focus和@blur的問題,上午查資料,各種折騰,還是沒有解決。最後請教了大神,上來就直接給我推翻了我的判斷。。。然後直接讓我百度了‘vue中對象屬性改變視圖不更新’,最後成功解決了問題!記錄一下解決vue中對象屬性改變視圖不更新的問題的方法
無法更新的方法:
// 數量輸入框失焦事件
private partBlur(index: any) {
const temp = this.materialList[index];
temp.partStatus = false;
}
這裏直接修改對象的屬性,然而在視圖中無法更新。
- 利用Vue.set(object,key,val)
// 數量輸入框失焦事件
private partBlur(index: any) {
const temp = this.materialList[index];
Vue.set(temp,'partStatus',false);
console.log(temp.partStatus);
}
- this.$set(this.obj,key,val)
// 數量輸入框失焦事件
private partBlur(index: any) {
const temp = this.materialList[index];
this.$set(temp,'partStatus',false);
console.log(temp.partStatus);
}
- Object.assign({},this.obj)
// 數量輸入框失焦事件
private partBlur(index: any) {
const temp = this.materialList[index];
temp.partStatus = false;
temp = temp.assign({},temp);
console.log(temp.partStatus);
}
- 先刪除再添加,即替換
// 數量輸入框失焦事件
private partBlur(index: any) {
const temp = this.materialList[index];
temp.partStatus = false;
this.materialList.splice(index,1,temp);
console.log(temp.partStatus);
}
上面的四種方法是在網上搜索得到的,我這裏是用最後一種才解決了。