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

vue2中watch的用法(通俗易懂,簡(jiǎn)單明了)

 更新時(shí)間:2023年09月14日 09:36:59   作者:coderkey  
這篇文章主要給大家介紹了關(guān)于vue2中watch用法的相關(guān)資料,通過(guò)watch監(jiān)聽(tīng)器,我們可以實(shí)時(shí)監(jiān)控?cái)?shù)據(jù)的變化,并且在數(shù)據(jù)發(fā)生改變時(shí)進(jìn)行相應(yīng)的操作,需要的朋友可以參考下

一,監(jiān)聽(tīng)基本普通屬性:

字符串,布爾值,number

(1)把要監(jiān)聽(tīng)的msg值看作方法名,來(lái)進(jìn)行監(jiān)聽(tīng)。

<template>
  <div id="app">
    <div>
      <button @click="btnClick">觸發(fā)</button>
      <div>{{ msg }}</div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      msg: "codekey",
    }
  },
  methods: {
    btnClick() {
      this.msg = 'pink'
    }
  },
  watch: {
    // watch第一次綁定值的時(shí)候不會(huì)執(zhí)行監(jiān)聽(tīng),修改數(shù)據(jù)才會(huì)觸發(fā)函數(shù)
    msg(newVal,oldVal) {
      console.log('oldVal:',oldVal)   // coderkey
      console.log('newVal:',newVal)   // pink
    }
  }
};
</script>

(2)把要監(jiān)聽(tīng)的msg值看作對(duì)象,利用hanler方法來(lái)進(jìn)行監(jiān)聽(tīng)

watch第一次綁定值的時(shí)候不會(huì)執(zhí)行監(jiān)聽(tīng),如果需要第一次就執(zhí)行監(jiān)聽(tīng) 則需要設(shè)置: immediate: true

<template>
  <div id="app">
    <div>
      <button @click="btnClick">觸發(fā)</button>
      <div>{{ msg }}</div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      msg: "codekey",
    }
  },
  methods: {
    btnClick() {
      this.msg = 'pink'
    }
  },
  watch: {
   //  如果需要第一次就執(zhí)行監(jiān)聽(tīng) 則需要設(shè)置:immediate: true
    msg: {
      handler(newVal,oldVal) {
        console.log('oldVal:',oldVal)  
        console.log('newVal:',newVal)  
      },
      immediate: true
    }, 
  }
};
</script>

二,監(jiān)聽(tīng)對(duì)象:

(1)監(jiān)聽(tīng)對(duì)象需要用到深度監(jiān)聽(tīng),設(shè)置 deep:true

<template>
  <div id="app">
    <div>
      <button @click="btnClick">觸發(fā)</button>
      <div>{{ obj.name }}</div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      obj: {
        name: 'coderkey',
        age: 18
      },
    }
  },
  methods: {
    btnClick() {
     this.obj.name = 'pink'
    }
  },
  watch: {
    obj:{
    //  注意:屬性值發(fā)生變化后,handler執(zhí)行后獲取的 newVal 值和 oldVal 值是一樣的
      handler(newVal,oldVal) {
        console.log('oldVal:',oldVal)
        console.log('newVal:',newVal)
      },
      immediate: true,
      // 開(kāi)啟深度監(jiān)聽(tīng) deep
      deep: true
    }
  }
};
</script>

(2)可以只監(jiān)聽(tīng)對(duì)象的其中一個(gè)屬性值 ’對(duì)象.屬性‘ 的形式

<template>
  <div id="app">
    <div>
      <button @click="btnClick">觸發(fā)</button>
      <div>{{ obj.name }}</div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      obj: {
        name: 'coderkey',
        age: 18
      },
    }
  },
  methods: {
    btnClick() {
     this.obj.name = 'pink'
    }
  },
  watch: {
    // 函數(shù)執(zhí)行后,獲取的 newVal 值和 oldVal 值不一樣
    'obj.name'(newVal,oldVal) {
      console.log('oldVal:',oldVal)    // coderkey
      console.log('newVal:',newVal)   // pink
    }, 
   /*  'obj.name':{
      handler(newVal,oldVal) {
        console.log('oldVal:',oldVal)
        console.log('newVal:',newVal)
      },
    // 立即處理 進(jìn)入頁(yè)面就觸發(fā)
      immediate: true
     }  */
  }
}
</script>

三,監(jiān)聽(tīng)數(shù)組

(1)(一維、多維)數(shù)組的變化不需要深度監(jiān)聽(tīng)

<template>
  <div id="app">
    <div>
      <button @click="btnClick">觸發(fā)</button>
      <!-- <div>{{ obj.name }}</div> -->
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
     arr1:[1,2,4,5,6]
    }
  },
  methods: {
    btnClick() {
      this.arr1.unshift(0)
    },
  },
  watch: {
    arr1:{
      handler(newVal,oldVal) {
        console.log('oldVal:',oldVal)    // [0,1,2,4,5,6]
        console.log('newVal:',newVal)   // [0,1,2,4,5,6]
      },
      // immediate: true,
     } 
  }
}
</script>

多維數(shù)組

<template>
  <div id="app">
    <div>
      <button @click="btnClick">觸發(fā)</button>
      <!-- <div>{{ obj.name }}</div> -->
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
     arr1:[1,2,4,[5,6]]
    }
  },
  methods: {
    btnClick() {
      this.arr1.push(0)
    },
  },
  watch: {
    arr1:{
      handler(newVal,oldVal) {
        console.log('oldVal:',oldVal)
        console.log('newVal:',newVal)
      },
      // immediate: true,
     } 
  }
}
</script>

(2)數(shù)組對(duì)象中對(duì)象屬性變化監(jiān)測(cè)需要使用 deep:true 深度監(jiān)聽(tīng),多少層內(nèi)產(chǎn)生變化都可以監(jiān)測(cè)到。

<template>
  <div id="app">
    <div>
      <button @click="btnClick">觸發(fā)</button>
      <!-- <div>{{ obj.name }}</div> -->
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      arr1: [
				{
					name: 'coderkey',
					age: 22
				}
			],
			arr2: [
				{
					name: 'coderkey',
					age: 22,
					children: [
						{
							name: 'why',
							sex: 33
						}
					]
				}
			]
    }
  },
  methods: {
    btnClick() {
      this.arr1[0].name = 'pink'
      this.arr2[0].children[0].name = 'pink'
    },
  },
  watch: {
    arr1: {
			handler(newVal, oldVal) {
				console.log('newVal1', newVal);
				console.log('oldVal1', oldVal);
			},
			deep: true
		},
		arr2: {
			handler(newVal, oldVal) {
				console.log('newVal2', newVal);
				console.log('oldVal2', oldVal);
			},
			deep: true
		}
  }
}
</script>

四,監(jiān)聽(tīng)路由變化

// 方法一:
watch:{
  $router(to,from){
       console.log(to.path)
  }
}
// 方法二:
watch: {
   $route: {
     handler:  function (val, oldVal){
       console.log(val);
     },
     // 深度觀察監(jiān)聽(tīng)
     deep:  true
   }
},
// 方法三:
watch: {
   '$route' : 'getRoutePath'
},
methods: {
   getRoutePath(){
     console.log( this .$route.path);
   }
}

總結(jié) 

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

相關(guān)文章

  • Vue中的reactive函數(shù)操作代碼

    Vue中的reactive函數(shù)操作代碼

    這篇文章主要介紹了Vue中的reactive函數(shù),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • 淺談vue的props,data,computed變化對(duì)組件更新的影響

    淺談vue的props,data,computed變化對(duì)組件更新的影響

    本篇文章主要介紹了淺談vue的props,data,computed變化對(duì)組件更新的影響,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • vue常見(jiàn)的通信方式總結(jié)

    vue常見(jiàn)的通信方式總結(jié)

    我們?nèi)粘m?xiàng)目開(kāi)發(fā)中,少不了組件之間的通信,我們可能只知道一些常見(jiàn)的方式比如props,emits,其實(shí),實(shí)現(xiàn)組件間的通信有很多種方式,本文就給大家總結(jié)一些我們常見(jiàn)的通信方式,需要的朋友可以參考下
    2023-08-08
  • vue之監(jiān)聽(tīng)頁(yè)面是否以到底部

    vue之監(jiān)聽(tīng)頁(yè)面是否以到底部

    這篇文章主要介紹了vue之監(jiān)聽(tīng)頁(yè)面是否以到底部問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • 詳解在Vue中通過(guò)自定義指令獲取dom元素

    詳解在Vue中通過(guò)自定義指令獲取dom元素

    本篇文章主要介紹了詳解在Vue中通過(guò)自定義指令獲取dom元素 ,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-03-03
  • Vue3使用vant檢索組件van-search遇到的問(wèn)題小結(jié)

    Vue3使用vant檢索組件van-search遇到的問(wèn)題小結(jié)

    當(dāng)清空按鈕與檢索按鈕同時(shí)居右時(shí),點(diǎn)擊clear清空按鈕事件時(shí)會(huì)同時(shí)觸發(fā)click-right-icon事件,這個(gè)時(shí)候容易觸發(fā)一系列問(wèn)題,小編小編給大家分享Vue3使用vant檢索組件van-search遇到的問(wèn)題小結(jié),感興趣的朋友一起看看吧
    2024-02-02
  • element-ui Upload上傳組件動(dòng)態(tài)配置action方式

    element-ui Upload上傳組件動(dòng)態(tài)配置action方式

    這篇文章主要介紹了element-ui Upload上傳組件動(dòng)態(tài)配置action方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • element-ui?table表格控件實(shí)現(xiàn)單選功能代碼實(shí)例

    element-ui?table表格控件實(shí)現(xiàn)單選功能代碼實(shí)例

    這篇文章主要給大家介紹了關(guān)于element-ui?table表格控件實(shí)現(xiàn)單選功能的相關(guān)資料,單選框是指在?Element?UI?的表格組件中,可以通過(guò)單選框來(lái)選擇一行數(shù)據(jù)。用戶只能選擇一行數(shù)據(jù),而不能同時(shí)選擇多行,需要的朋友可以參考下
    2023-09-09
  • vue轉(zhuǎn)react入門(mén)指南

    vue轉(zhuǎn)react入門(mén)指南

    因?yàn)樾鹿臼褂胷eact技術(shù)棧,包括Umi、Dva、Ant-design等一系列解決方案。本文就簡(jiǎn)單的介紹一下vue轉(zhuǎn)react入門(mén)指南,感興趣的可以了解一下
    2021-10-10
  • elementui之el-table如何通過(guò)v-if控制按鈕顯示與隱藏

    elementui之el-table如何通過(guò)v-if控制按鈕顯示與隱藏

    這篇文章主要介紹了elementui之el-table如何通過(guò)v-if控制按鈕顯示與隱藏問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11

最新評(píng)論