解决vue中对象属性改变视图不更新的问题

今天碰到了一个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);
  }

上面的四种方法是在网上搜索得到的,我这里是用最后一种才解决了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章