Vue2中watch偵聽器

watch偵聽器作用


定義一個(gè)對(duì)象來(lái)創(chuàng)建監(jiān)聽器。這個(gè)對(duì)象包含要監(jiān)聽的數(shù)據(jù)屬性名稱及其對(duì)應(yīng)的回調(diào)函數(shù)。通過監(jiān)聽器來(lái)監(jiān)聽對(duì)象,來(lái)實(shí)現(xiàn)數(shù)據(jù)的前后變化的強(qiáng)制同步。

基礎(chǔ)用法


  • 方式1
<template>
    <div class="test">
      姓名:<el-input v-model="userName" style="width:30%;" size="medium"></el-input>
    </div>
</template>
    
<script>
  export default {
    components: {},
    name: "test",
    data() {
      return {
        userName: "張三",
      };
    },
    watch: {
      userName(newVal, oldVal) {
        console.log("newVal = " + newVal);
        console.log("oldVal = " + oldVal);
      }
    },
    mounted() {},
    beforeCreate() {},
    created() {},
    methods: {},
  };
</script>
<style scoped></style>
  • 方式2
watch: {
  userName : {
    handler(newVal, oldVal) {
      console.log("newVal = " + newVal);
      console.log("oldVal = " + oldVal);
    }
  }
},

immediate用法


  • immediate屬性為true時(shí),一直都會(huì)監(jiān)聽。
  • immediate屬性為false時(shí),默認(rèn)用法,值發(fā)生改變才監(jiān)聽。
<template>
    <div class="test">
      姓名:<el-input v-model="userName" style="width:30%;" size="medium"></el-input>
    </div>
</template>
    
<script>
  export default {
    components: {},
    name: "test",
    data() {
      return {
        userName: "張三",
      };
    },
    watch: {
      userName : {
        handler(newVal, oldVal) {
          console.log("newVal = " + newVal);
          console.log("oldVal = " + oldVal);
        },
        immediate: true
      }
    },
    mounted() {},
    beforeCreate() {},
    created() {},
    methods: {},
  };
</script>
<style scoped></style>

監(jiān)聽對(duì)象和數(shù)組情況


監(jiān)聽對(duì)象

  • deep屬性值為true時(shí),監(jiān)聽對(duì)象整個(gè)內(nèi)部的屬性;
  • deep屬性值為false時(shí),則監(jiān)聽不到對(duì)象整個(gè)內(nèi)部的屬性。
<template>
    <div class="test">
      地址:<el-input v-model="tempUserObj.info.address" style="width:30%;" size="medium"></el-input>
    </div>
</template>
    
<script>
  export default {
    components: {},
    name: "test",
    data() {
      return {
        tempUserObj: {
          "id" : "123456",
          "family" : ["father", "sister", "brother"],
          "info" : {
            "age" : 30,
            "address" : "吉林省長(zhǎng)春市朝陽(yáng)區(qū)"
          },
        },
      };
    },
    watch: {
      tempUserObj : {
        handler(newVal, oldVal) {
          console.log("newVal = " + JSON.stringify(newVal));
          console.log("oldVal = " + JSON.stringify(oldVal));
        },
        immediate: true,
        deep: true
      }
    },
    mounted() {},
    beforeCreate() {},
    created() {},
    methods: {},
  };
</script>
<style scoped></style>

監(jiān)聽數(shù)組

watch: {
  familyArr : {
    handler(newVal, oldVal) {
      console.log("newVal = " + JSON.stringify(newVal));
      console.log("oldVal = " + JSON.stringify(oldVal));
    },
    deep: true
  }
},
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容