Skip to content

前端-由于 JavaScript 的限制,Vue 不能检测对象属性的添加或删除(会出现修改某个多层对象值时,视图没有更新等问题) #19

Description

@agger21

在一个绑定对象中添加一个响应式属性:

例如:
var vm = new Vue({
  data: {
    userProfile: {
      name: 'Anika'
    }
  }
})

vm.$set(vm.userProfile, 'age', 27)

有时你可能需要为已有对象赋予多个新属性,比如使用 Object.assign() 或 _.extend()。在这种情况下,你应该用两个对象的属性创建一个新的对象。

例如:
vm.userProfile = Object.assign({}, vm.userProfile, {
  age: 27,
  favoriteColor: 'Vue Green'
})
用watch监听绑定属性,通过设置深度监听属性来监听属性变化。
watch:{
   xxx:{
      handler(newVal,odlVal){
          console.log(newVal,'为更新后的值');
          console.log(odlVal,'为更新前的值');
     }, 
     deep:true
   }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions