Vue3中watch監(jiān)聽使用詳解
Vue2使用watch
<template>
<div>總合:{{ sum }}<button @click="sum++">點(diǎn)擊累加</button></div>
</template>
<script>
import { ref } from "vue";
export default {
// vue2中使用watch
watch: {
sum: {
deep: true,
handler(newValue, oldValue) {
console.log("總合 sum 變化:", newValue, oldValue);
},
},
},
setup() {
let sum = ref(0);
return {
sum,
};
},
};
</script>
<style>
</style>
Vue3使用watch
watch有三個(gè)參數(shù):
參數(shù)1:監(jiān)聽的參數(shù)
參數(shù)2:監(jiān)聽的回調(diào)函數(shù)
參數(shù)3:監(jiān)聽的配置(immediate)
情況1
監(jiān)視r(shí)ef所定義的一個(gè)響應(yīng)式數(shù)據(jù)
<template>
<div>總合:{{ sum }}<button @click="sum++">點(diǎn)擊累加</button></div>
</template>
// 監(jiān)視r(shí)ef所定義的一個(gè)響應(yīng)式數(shù)據(jù)
<script>
import { ref, watch } from "vue";
export default {
setup() {
let sum = ref(0);
// 監(jiān)視r(shí)ef所定義的一個(gè)響應(yīng)式數(shù)據(jù)
watch(sum, (newValue, oldValue) => {
console.log("sum ==> ", newValue, oldValue);
});
return {
sum,
};
},
};
</script>

情況2
監(jiān)視r(shí)ef所定義的多個(gè)響應(yīng)式數(shù)據(jù)
<template>
<div>總合:{{ sum }}<button @click="sum++">點(diǎn)擊累加</button></div>
<hr />
<div>
msg:{{ msg }}
<button @click="msg += '~'">改變msg</button>
</div>
</template>
<script>
import { ref, watch } from "vue";
export default {
setup() {
let sum = ref(0);
let msg = ref("watch使用"):
// 情況2:監(jiān)視r(shí)ef所定義的多個(gè)響應(yīng)式數(shù)據(jù)
watch([sum, msg], (newValue, oldValue) => {
console.log("sum/msg ==> ", newValue, oldValue);
},{immediate:true});
return {
sum,
msg,
};
},
};
</script>

情況3
監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)
注意:
- 這里無法正確獲取oldValue
- 強(qiáng)制開啟了深度監(jiān)聽(deep配置不生效)
<template>
<div>
<h3>情況3::監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)</h3>
<div>姓名:{{person.name}}</div>
<div>年齡:{{person.age}}</div>
<button @click="person.name += '~'">修改姓名</button>
<button @click="person.age ++">修改年齡</button>
</div>
</template>
<script>
import { ref, watch,reactive } from "vue";
export default {
setup() {
let person = reactive({
name: "lisa",
age: 18,
job: {
joblist: {
money: 10,
},
},
});
// 情況3、監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)
/*
若watch監(jiān)視的是reactive定義的響應(yīng)式數(shù)據(jù),則無法正確獲得oldvalue!
若watch監(jiān)視的是reactive定義的響應(yīng)式數(shù)據(jù),則強(qiáng)制開啟了深度監(jiān)視
*/
watch(person,(newValue, oldValue) => {
console.log("person ==> ", newValue, oldValue);
},{immediate:true,deep:false}//這里的deep配置不再奏效
);
return {
person,
};
},
};
</script>
情況4
監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)中的某個(gè)屬性
<template>
<div>
<h3>情況4::監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)中的某個(gè)屬性</h3>
<div>姓名:{{person.name}}</div>
<div>年齡:{{person.age}}</div>
<button @click="person.name += '~'">修改姓名</button>
<button @click="person.age ++">修改年齡</button>
</div>
</template>
<script>
import { ref, watch,reactive } from "vue";
export default {
setup() {
let person = reactive({
name: "lisa",
age: 18,
job: {
joblist: {
money: 10,
},
},
});
// 情況4、監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)中的某個(gè)屬性
watch(()=>person.name,(newValue, oldValue) => {
console.log("person.name ==> ", newValue, oldValue);
});
return {
person,
};
},
};
</script>

情況5
監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)中的某些屬性
<template>
<div>
<h3>情況4::監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)中的某個(gè)屬性</h3>
<div>姓名:{{person.name}}</div>
<div>年齡:{{person.age}}</div>
<button @click="person.name += '~'">修改姓名</button>
<button @click="person.age ++">修改年齡</button>
</div>
</template>
<script>
import { ref, watch,reactive } from "vue";
export default {
setup() {
let person = reactive({
name: "lisa",
age: 18,
job: {
joblist: {
money: 10,
},
},
});
// 情況5、監(jiān)視r(shí)eactive所定義的一個(gè)響應(yīng)式數(shù)據(jù)中的某些屬性
watch([()=>person.name,()=>person.age],(newValue, oldValue) => {
console.log("person.name/person.age ==> ", newValue, oldValue);
});
return {
person,
};
},
};
</script>

特殊情況
watch監(jiān)聽reactive中對(duì)象的嵌套對(duì)象
<template>
<div>
<div>姓名:{{person.name}}</div>
<div>年齡:{{person.age}}</div>
<div>薪資:{{person.job.joblist.money}} K</div>
<button @click="person.name += '~'">修改姓名</button>
<button @click="person.age ++">修改年齡</button>
<button @click="person.job.joblist.money ++">提薪</button>
</div>
</template>
<script>
import { ref, watch,reactive } from "vue";
export default {
setup() {
let person = reactive({
name: "lisa",
age: 18,
job: {
joblist: {
money: 10,
},
},
});
// 特殊情況、監(jiān)視r(shí)eactive所定義嵌套對(duì)象
watch(()=>person.job,(newValue, oldValue) => {
console.log("person.job對(duì)象發(fā)生變化 ==> ", newValue, oldValue);
},{deep:true});//此處由于監(jiān)視的是reactive素定義的對(duì)象中的某個(gè)屬性,所以deep配置有效
return {
person,
};
},
};
</script>
總結(jié)
到此這篇關(guān)于Vue3中watch監(jiān)聽使用詳解的文章就介紹到這了,更多相關(guān)Vue3中watch使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項(xiàng)目實(shí)現(xiàn)減少app.js和vender.js的體積操作
這篇文章主要介紹了vue項(xiàng)目實(shí)現(xiàn)減少app.js和vender.js的體積操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
vue實(shí)現(xiàn)滑動(dòng)驗(yàn)證條
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)滑動(dòng)驗(yàn)證條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Vue中對(duì)拿到的數(shù)據(jù)進(jìn)行A-Z排序的實(shí)例
今天小編就為大家分享一篇Vue中對(duì)拿到的數(shù)據(jù)進(jìn)行A-Z排序的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09
淺析Vue中權(quán)限管理的實(shí)現(xiàn)
在前端開發(fā)中,權(quán)限管理是一項(xiàng)至關(guān)重要的任務(wù),本教程將深入探討如何在?Vue.js?項(xiàng)目中實(shí)施權(quán)限管理,并詳細(xì)講解如何實(shí)現(xiàn)到按鈕級(jí)別的細(xì)粒度控制,希望對(duì)大家有所幫助2024-11-11
vue-router項(xiàng)目實(shí)戰(zhàn)總結(jié)篇
vue-router 是 Vue.js 官方的路由庫(kù).這篇文章主要介紹了vue-router項(xiàng)目實(shí)戰(zhàn)總結(jié),需要的朋友可以參考下2018-02-02
antd-DatePicker組件獲取時(shí)間值,及相關(guān)設(shè)置方式
這篇文章主要介紹了antd-DatePicker組件獲取時(shí)間值,及相關(guān)設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
vue中使用v-if,v-else來設(shè)置css樣式的步驟
我們?cè)谑褂胿ue項(xiàng)目開發(fā)時(shí),v-if是使用的非常多的,在這里我們談?wù)勅绾问褂胿-i來綁定修改css樣式,使用的主要是雙向數(shù)據(jù)綁定,即通過改變他的狀態(tài)來改變他的樣式,這篇文章主要介紹了vue中如何使用v-if,v-else來設(shè)置css樣式,需要的朋友可以參考下2023-03-03
在uniapp中實(shí)現(xiàn)圖形驗(yàn)證碼的詳細(xì)步驟
圖形驗(yàn)證碼是一種常見的安全措施,用于防止自動(dòng)化軟件(機(jī)器人)濫用網(wǎng)站資源,如自動(dòng)提交表單,這篇文章主要介紹了在uniapp中實(shí)現(xiàn)圖形驗(yàn)證碼的詳細(xì)步驟,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-11-11
記一次vue-webpack項(xiàng)目?jī)?yōu)化實(shí)踐詳解
這篇文章主要介紹了記一次vue-webpack項(xiàng)目?jī)?yōu)化實(shí)踐,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02

