Vue.watch监视器事件

watch监视器事件

案例

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
     <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.2.47/vue.global.prod.js"></script>
     <style>
       #app div,h1{
         margin: 40px auto;
         text-align: center;
       }
     </style>
  </head>
  <body>
    <div id="app">
       <h1>watch监听简单数据类型</h1>
       <div class="example1">
         千米:<input type="text" v-model.number.lazy="kilo"/>
         <=>
         米:<input type="text" v-model.number.lazy="meter"/>
         <p>左输入框修改前为:{{oldKilo}} ,修改后为:{{newKilo}}</p>
         <p>右输入框修改前为:{{oldMeter}},修改后为:{{newMeter}}</p>
       </div>
       <h1>watch深度监听</h1>
       <div class="example2">
           用户名:<input type="text" v-model.lazy="user.name"/>
           年龄:<input type="text" v-model.lazy="user.age"/>
       </div>
    </div>
    <script>
      const app=Vue.createApp({
        data(){
          return {
              kilo:0,
              meter:0,
              oldKilo:0,
              newKilo:0,
              oldMeter:0,
              newMeter:0,
              user:{
                  name:"张三",
                  age:22
              }
          }
        },
        watch:{
            kilo(newVal,oldVal){
                this.oldKilo = oldVal;
                this.newKilo = newVal;
                this.meter = newVal*1000;
            },
            meter(newVal,oldVal){
                this.oldMeter = oldVal;
                this.newMeter = newVal;
                this.kilo = newVal/1000;
            },
            // 无法对对象进行深度监听 无法监听到对象的属性变化 无法监听到数组的变化
            // user(newVal,oldVal){
            //     console.log("newVal:",newVal);
            //     console.log("oldVal:",oldVal);
            // }

            // "user.age"(newVal,oldVal){
            //     console.log("新user.age变化了"+newVal);
            //     console.log("旧user.age变化了"+oldVal);
            // },
            // "user.name"(newVal,oldVal){
            //     console.log("新user.name变化了"+newVal);
            //     console.log("旧user.name变化了"+oldVal);
            // }

            user:{
                // handler 监听的回调函数
                handler(newVal,oldVal){
                    console.log("new newVal:",newVal);
                    console.log("old oldVal:",oldVal);
                },
                // deep:true 深度监听 默认false 只能监听到一层
                deep:true,
                // immediate:true 立即执行一次 默认false 只有在值发生变化时才会执行
                immediate:true
            }
        },
          computed:{
                newUser(){
                    return JSON.Parse(JSON.Stringify(this.user));
                }
          },
      }).mount("#app");
    </script>
  </body>
</html>

监听属性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱捣鼓的XiaoPu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值