vue.watch的触发条件是什么

很多人习惯用watch,但是却很少有人知道watch的真正触发条件。如果不是对vue原理了如指掌,请谨慎使用watch。

示例1,下面会触发watch 吗?

<script>
  new Vue({
    data() {
      return {
        city: {id: 1, name: 'Beijing'}
      }
    },
    watch: {
      city() {
        console.log('city changed')
      }
    },
    created() {
      this.city = {id: 1, name: 'Beijing'}
    }
  })
</script>

会触发,因为在created方法里面重新给city赋值了一个对象,city前后的指向不同了

示例2:

<script>
  new Vue({
    data() {
      return {
        city: {id: 1, name: 'Beijing'}
      }
    },
    watch: {
      city() {
        console.log('city changed')
      }
    },
    created() {
      this.city.name = 'Shanghai'
    }
  })
</script>

不会触发, 因为created方法执行之后, city的指向没有变
如果我们期望捕获这种更新,应该这样写代码:

watch: {
    city: {
        handler: () => console.log('city changed'),
        deep: true
    }
}

将选项deep设为true能让vue捕获对象内部的变化。
下面讨论一下watch一个数组:

<script>
new Vue({
    el: '#body',
    data() {
        return {
            cities: ['Beijing', 'Tianjin']
        }
    },
    watch: {
        cities() {
            console.log('cities changed')
        }
    }
})
</script>

那下面哪些操作会触发cities的watch回调呢?

this.cities = ['Beijing', 'Tianjin']
this.cities.push('Xiamen')
this.cities = this.cities.slice(0, 1)
this.cities.pop();
this.cities.sort((a,b)=>a.localeCompare(b));
this.cities[0] = 'Shenzhen'
this.cities.splice(0, 1)
this.cities.length = 0

答案是只有最后三行不会触发。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值