网络日志

【Vue】watch

完整可运行代码

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
  <div id="app">
    <input v-model="info.hobby">
    <p> {{ info }} </p>
    <input v-model="name">
    <p> {{ name }} </p>
  </div>
</body>
<script>
  var app = new Vue({
    el: '#app',
    data: {
      name: 'tao',
      info: {
        hobby: 'reading',
      }
    },
    watch: {
      name(newV, oldV) {
        console.log('name', newV, oldV)
      },
      // info(newV, oldV) {
      //   console.log('info', newV, oldV)
      // },
      // 深度监听对象
      info: {
        handler: function(newV, oldV) {
          // 新老值引用的是同一个对象
          console.log('info', newV, oldV)
        },
        deep: true,
      }
    }
  })
</script>
</html>