VUE整理

44 阅读1分钟

computed和watch

一、watch

watch: {
  counter: {
    handler: function(newValue, oldValue){
      if(this.counter == 10){
        this.counter = 0;
      }
    },
    immediate: true, //侦听属性对应的函数立即执行
    deep:true //侦听到对象内部属性的变化
  }
}

二、computed

computed: { //计算属性不能和 Vue Data属性同名
    hours:{ 
        get: function() { //根据依赖关系进行计算并缓存, 只有当依赖被改变的时候才会更新
            return this.minute / 60; 
        }, 
        set: function(newValue) { //主动修改了`计算属性`的值,`set`方法才会被触发
            this.minute = newValue * 60; 
        } 
    } 
 }

[1](juejin.cn/post/691780…)

[2](juejin.cn/post/712561…)