欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

VUE中watch的詳細使用教程(推薦!)

 更新時間:2023年08月11日 11:31:51   作者:寵297  
這篇文章主要給大家介紹了關(guān)于VUE中watch的詳細使用教程,watch是vue實例的一個屬性,主要用來監(jiān)聽數(shù)據(jù)的變化,并做出一些操作,需要的朋友可以參考下

1、watch是什么?

watch:是vue中常用的偵聽器(監(jiān)聽器),用來監(jiān)聽數(shù)據(jù)的變化

2、watch的使用方式如下

watch: {

        這里寫你在data中定義的變量名或別處方法名: {

                handler(數(shù)據(jù)改變后新的值, 數(shù)據(jù)改變之前舊的值) {

                                這里寫你拿到變化值后的邏輯

                        }

                }

        }

3、watch監(jiān)聽簡單案例(監(jiān)聽一個)

<template>
  <div>
    <div>
      <input type="text" v-model="something">
    </div>
  </div>
</template>
<script>
  export default {
    name: "AboutView",
    components: {},
    data() {
      return {
         something: ""
      }
    },
    watch: {
        //方法1
       "something"(newVal, oldVal) {
          console.log(`新值:${newVal}`);
          console.log(`舊值:${oldVal}`);
          console.log("hellow  world");
      }
        //方法2
        "something": {
            handler(newVal, oldVal) {
              console.log(`新的值: ${newVal}`);
              console.log(`舊的值: ${oldVal}`);
              console.log("hellow  world");
            }
          }
        }
      }
</script>

在輸入框中輸入1、4   效果圖如下:

4、watch監(jiān)聽復雜單一案例(例:監(jiān)聽對象中的某一項)

<template>
  <div>
    <div>
      <input type="text" v-model="obj.something">
    </div>
  </div>
</template>
<script>
  export default {
    name: "AboutView",
    components: {},
    data() {
      return {
         obj: {
           something: ""
        }
      }
    },
    watch: {
        "obj.something": {
            handler(newVal, oldVal) {
              console.log(`新的值: ${newVal}`);
              console.log(`舊的值: ${oldVal}`);
              console.log("hellow  world");
            }
          }
        }
      }
</script>

 在輸入框中輸入4、5   效果圖如下:

5、watch中immediate的用法和作用

1、作用:immediate頁面進來就會立即執(zhí)行,值需要設為true

2、用法如下方代碼所示:

<template>
  <div>
    <div>
      <input type="text" v-model="obj.something">
    </div>
  </div>
</template>
<script>
  export default {
    name: "AboutView",
    components: {},
    data() {
      return {
         obj: {
           something: ""
        }
      }
    },
    watch: {
        "obj.something": {
            handler(newVal, oldVal) {
              console.log(`新的值: ${newVal}`);
              console.log(`舊的值: ${oldVal}`);
              console.log("hellow  world");
            },
            immediate:true
          }
        }
      }
</script>

進來頁面后立即加載,效果圖如下:

6、watch中deep 深度監(jiān)聽的用法和作用

1、作用:deep 用來監(jiān)聽data中的對象,值需要設為true

2、用法如下方代碼所示:

<template>
  <div>
    <div>
      <input type="text" v-model="obj.something">
    </div>
  </div>
</template>
<script>
  export default {
    name: "AboutView",
    components: {},
    data() {
      return {
         obj: {
           something: ""
        }
      }
    },
    watch: {
        "obj": {
            handler(newVal, oldVal) {
              console.log(`新的值: ${newVal}`);
              console.log(`舊的值: ${oldVal}`);
              console.log("hellow  world");
            },
            deep:true
          }
        }
      }
</script>

注意:

1、console.log(`新的值: ${newVal}`); 這種打印出來的是對象的類型,如下圖:

 2、console.log(newVal);這種打印出來的是對象本身,如下圖:

總結(jié) 

到此這篇關(guān)于VUE中watch的詳細使用教程的文章就介紹到這了,更多相關(guān)VUE watch使用教程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論