vuejs深度监听代码介绍

2025年02月19日 建站教程

vuejs如何实现深度监听,下面web建站小编给大家简单介绍一下代码!

深度监听代码如下:

<body>
  <div id="root">
    <div>a的值是{{numbers.a}}</div>
    <button @click="numbers.a++">点我加1</button>
  </div>
  <script>
    Vue.config.productionTip = false;
  
    new Vue({
      el:"#root",
      data:{
        numbers:{
          a:1,
          b:1
        }
      },
      watch:{
        "numbers.a":{
          handler(newValue,oldValue){
            console.log("a:","newValue="+newValue,"oldValue="+oldValue);
          }
        },
        "numbers":{
          deep:true,
          handler(newValue,oldValue){
            console.log("numbers发生了变化!");
          }
        }
      }
    })
  </script>
</body>

本文链接:http://so.lmcjl.com/news/23465/

展开阅读全文
相关内容