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

admin3条评论 120 次浏览

常规情况下我们在vue实例的data中设置响应数据。但当数据为对象,我们增加或删除对象属性值时,视图并不触发更新,如何解决这个问题呢?

let vm = new Vue{

el: ‘#app’,

   data: {

    obj: { k: ‘v’ }

  }, 

}

 

有三种解决方案:
方案一:利用Vue.set(object,key,val)

例:Vue.set(vm.obj,’k1′,’v1′)

方案二:利用this.$set(this.obj,key,val)

this.$set(this.obj,’k1′,’v1′)

方案三:利用Object.assign({},this.obj)创建新对象

例: this.obj.k1=’v1’; this.obj = Object.assign({}, this.obj) 或 this.obj = Object.assign({}, this.obj,{‘k1′,’v1’})


3 thoughts on “ vue中对象属性改变视图不更新问题 ”

  1. d scr888 fun

    Possibly you have to pay to use such a keyword tool
    but for my part it makes it worth while. First, analyze the website
    your link will go on. Link exchanging with other blogs which have in similar market as yours. https://fury.cse.buffalo.edu/questions/index.php?qa=user&qa_1=stephen23

  2. Cash Loan

    lender loans

发表评论

? razz sad evil ! smile oops grin eek shock ??? cool lol mad twisted roll wink idea arrow neutral cry mrgreen

分类目录

分类目录