Vue之watch监听对象中某个属性的方法

新建 userinfo = { name: "小明",  age: "18", }

  vue中watch监听name的方法

  1. 可以结合计算属性的方法实现

{
  ......
   watch: {
     nm () {
       console.log(this.nm)
     }
   },
    computed: {
      nm () { return this.userinfo.name }
   }
  ......  
}

  2. 可以通过配置 deep 为true实现

// 监听对象的某个值
{
  ......
   watch: {
     'userinfo.name' () {
       console.log(this.nm)
     }
   }
  ......  
}

// 直接监听整个属性,消耗大
{
  ......
   watch: {
     userinfo () {
       handler () {
           console.log(this.nm)
       },
       deep: true
     }
   }
  ......  
}        

    

    

猜你喜欢

转载自www.cnblogs.com/jingxuan-li/p/11817329.html