【尚硅谷 Vue学习笔记】P22 监视(侦听)属性 P23 深度监视 p24监视的简写形式 P25watch对比computed


前言:仅做笔记使用。不是制作精良的博客。

时间:2022/11/15

P22 监视(侦听)属性

watch:{
        isHot:{
          //hander什么时候调用?当isHot发生改变时
          handler(){
            console.log('isHot被修改')
          }
        }
      }
immediate:false//初始化时让handler调用一下
<body>
  <!--准备好一个容器-->
  <div id="root">
    <h2>今天天气很 {{info}}</h2>
    <button @click="changeWeather">切换天气</button>
  </div>
  <script type="text/javascript">
    Vue.config.productionTip = false;
    const vm=new Vue({
      el: '#root',
      data: {
        isHot: true,
      },
      computed: {
        info() {
          return this.isHot ? '炎热' : '凉爽'
        }
      },
      methods: {
        changeWeather() {
          this.isHot = !this.isHot
        }
      },
    })
    vm.$watch('isHot',{
      handler(newValue,oldValue){
       console.log('isHot被修改',newValue,oldValue)
      }
    })
  </script>
</body>

image-20221115141957966

P23 深度监视

<body>
  <!--准备好一个容器-->
  <div id="root">
    <h2>今天天气很 {{info}}</h2>
    <button @click="changeWeather">切换天气</button>
    <hr>
    <h2>a的值是:{{numbers.a}}</h2>
    <button @click="numbers.a++">点我a++</button>
  </div>


  <script type="text/javascript">
    Vue.config.productionTip = false;
    new Vue({
      el: '#root',
      data: {
        isHot: true,
        numbers:{
          a:1,
          b:1
        }
      },
      computed: {
        info() {
          return this.isHot ? '炎热' : '凉爽'
        }
      },
      methods: {
        changeWeather() {
          this.isHot = !this.isHot
        }
      },
      watch:{
        isHot:{
          //hander什么时候调用?当isHot发生改变时
          handler(newValue,oldValue){
          console.log('isHot被修改',newValue,oldValue)
          }
        }
      }
    })
  </script>
</body>

image-20221115142905130

要求:检测a的值。

注意点:

watch:{
    //isHot是简写方式
    isHot:{
        
    }
    //完整方式:
    'isHot':{
        
    }
    //检测a
    //这样写是错误的
    numbers.a:{
        
    }
    //正确写法:完整方式
    'numbers.a':{
        
    }
}
//监视多级结构中某个属性的变化
        'numbers.a':{
          handler(){
            console.log('a被改变了')
          }
        }

想要监视numbers里面的数据的变化:

配置项:deep

//监视多级结构中所有属性的变化 
numbers:{
     //深度监视开启
          deep:true,
          handler(){
            console.log('numbers改变了')
          }
       }

image-20221115144334214

开启之后:点我a++/点我b++,都会有numbers改变了

p24监视的简写形式

 watch: {
        //正常:
        // isHot:{
        //   //handler什么时候调用?当isHot发生改变时
        //   handler(newValue,oldValue){
        //   console.log('isHot被修改',newValue,oldValue)
        //   }
        // }
        //简写:
        //只有hander的时候,比如没有deep
        isHot(newValue, oldValue) {
          console.log('isHot被修改', newValue, oldValue)
        }
      }
 vm.$watch('isHot',function(newValue,oldValue){
      console.log('isHot被修改',newValue,oldValue)
    })

P25watch对比computed

  <script type="text/javascript">
    Vue.config.productionTip=false;
    new Vue({
      el:'#root',
      data:{
        firstName:'张',
        lastName:'san',
        fullName:'张-san'
      },
      watch:{
        firstName(val){
          this.fullName=val+'-'+this.lastName
        },
        lastName(val){
          this.fullName=this.firstName+'-'+val
        }
      }

    })
  </script>

实现起来比较麻烦,但是我提个需求:等一秒钟再执行(异步计算)

firstName(val){
          //给个定时器
    	//定时器不是Vue所控制的
          setTimeout(()=>{
            this.fullName=val+'-'+this.lastName
          },1000);
          
        }

image-20221115152232191

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潮.eth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值