老生常談vue2中watch的使用
一,監(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);
}
}到此這篇關(guān)于老式常談vue2中watch的使用的文章就介紹到這了,更多相關(guān)vue2中watch使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue列表循環(huán)從指定下標(biāo)開(kāi)始的多種解決方案
這篇文章主要介紹了Vue列表循環(huán)從指定下標(biāo)開(kāi)始的多種方案,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
VeeValidate在vue項(xiàng)目里表單校驗(yàn)應(yīng)用案例
這篇文章主要介紹了VeeValidate在vue項(xiàng)目里表單校驗(yàn)應(yīng)用案例,VeeValidate是Vue.js的驗(yàn)證庫(kù),它有很多驗(yàn)證規(guī)則,并支持自定義規(guī)則,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-05-05
Vue 第三方字體圖標(biāo)引入 Font Awesome的方法
今天小編就為大家分享一篇Vue 第三方字體圖標(biāo)引入 Font Awesome的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Vue+ElementUI容器無(wú)法鋪滿網(wǎng)頁(yè)的問(wèn)題解決
這篇文章主要介紹了Vue+ElementUI容器無(wú)法鋪滿網(wǎng)頁(yè)的問(wèn)題解決,文章通過(guò)圖文結(jié)合的方式給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-08-08
Vue2+SpringBoot實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出到csv文件并下載的使用示例
本文主要介紹了Vue2+SpringBoot實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出到csv文件并下載,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-10-10
electron+vue?實(shí)現(xiàn)靜默打印功能
這篇文章主要介紹了electron+vue?實(shí)現(xiàn)靜默打印功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
daisyUI解決TailwindCSS堆砌class問(wèn)題詳解
這篇文章主要為大家介紹了daisyUI解決TailwindCSS堆砌class問(wèn)題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
詳解vue 單頁(yè)應(yīng)用(spa)前端路由實(shí)現(xiàn)原理
這篇文章主要介紹了詳解vue 單頁(yè)應(yīng)用(spa)前端路由實(shí)現(xiàn)原理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04

