使用Vue3的watch實(shí)現(xiàn)數(shù)據(jù)的實(shí)時(shí)更新(附詳細(xì)代碼)
watch函數(shù)用于偵聽某個(gè)值的變化,當(dāng)該值發(fā)生改變后,觸發(fā)對(duì)應(yīng)的處理邏輯。
一、監(jiān)聽基礎(chǔ)ref類型
1.監(jiān)聽單個(gè)ref數(shù)據(jù)
<template>
<button class="style" @click="num++">增加watch</button>
</template>
<script setup>
import { watch, ref } from "vue";
const num = ref(1);
// newVal: 新值 | oldVal: 舊值
watch(num, (newVal, oldVal) => {
console.log(`新值:${newVal} --- 舊值:${oldVal}`);
});
</script>
<style lang="scss" scoped>
.style {
margin: 20px 20px;
}
</style>
初始值為1 點(diǎn)擊按鈕后 偵聽到

2.監(jiān)聽多個(gè)ref數(shù)據(jù),以數(shù)組的形式偵聽
<template>
<h1 class="style">{{ one }} | {{ two }}</h1>
<button class="style" @click="one++">增加one的值</button>
<button class="style" @click="two++">增加two的值</button>
</template>
<script setup>
import { watch, ref } from "vue";
const one = ref(0);
const two = ref(10);
// newVal: 新值 | oldVal: 舊值
watch([one, two], ([newVal, oldVal]) => {
console.log(`新值:${newVal} --- 舊值:${oldVal}`);
});
</script>
<style lang="scss" scoped>
.style {
margin: 10px 20px;
}
</style>
one的初始值為1 two的初始值為10 點(diǎn)擊one按鈕后 偵聽到 one的值+1

點(diǎn)擊two按鈕后 偵聽到 two的值+1

3.監(jiān)聽一個(gè)ref對(duì)象
<template>
<h1 class="style">{{ num.number }}</h1>
<h1 class="style">{{ num.age }}</h1>
<button class="style" @click="num.number++">增加number的值</button>
</template>
<script setup>
import { watch,ref } from "vue";
const num = ref({
number: 1,
age: 18,
});
// newVal: 新值 | oldVal: 舊值
// 這個(gè)偵聽器無效,即控制臺(tái)無輸出
watch(num, (newVal, oldVal) => {
console.log("偵聽器1:", newVal, oldVal);
});
// getter函數(shù)形式,新舊值不一樣
watch(
() => ({ ...num.value }),
(newVal, oldVal) => {
console.log("偵聽器2:", newVal, oldVal);
}
);
</script>
<style lang="scss" scoped>
.style {
margin: 10px 20px;
}
</style>

二、監(jiān)聽基礎(chǔ)reactive類型
1.監(jiān)聽對(duì)象中的單個(gè)屬性
<template>
<h1 class="style">{{ num.value }}</h1>
<button class="style" @click="num.value++">增加one的值</button>
</template>
<script setup>
import { watch, reactive } from "vue";
const num = reactive({
value: 1,
});
// newVal: 新值 | oldVal: 舊值
watch(
() => num.value,
(newVal, oldVal) => {
console.log(`新值:${newVal} --- 舊值:${oldVal}`);
}
);
</script>
<style lang="scss" scoped>
.style {
margin: 10px 20px;
}
</style>

初始值為1 點(diǎn)擊按鈕后 偵聽到

2.多層嵌套的對(duì)象
<template>
<h1 class="style">{{ num.number }}|{{ num.key.age }}</h1>
<button class="style" @click="num.number++">增加number的值</button>
<button class="style" @click="num.key.age++">增加age的值</button>
</template>
<script setup>
import { watch, reactive } from "vue";
const num = reactive({
number: 1,
name: "張三",
key: {
age: 18,
},
});
// newVal: 新值 | oldVal: 舊值
watch(
[() => num.number, () => num.key.age],
(newVal, oldVal) => {
console.log(`新值:${newVal} --- 舊值:${oldVal}`);
});
</script>
<style lang="scss" scoped>
.style {
margin: 10px 20px;
}
</style>
number的初始值為1 點(diǎn)擊左側(cè)按鈕number+1 age的值不變

age的初始值為18 點(diǎn)擊右側(cè)按鈕age+1 number值不變

3.同時(shí)監(jiān)聽ref基本類型數(shù)據(jù)和reactive對(duì)象中的屬性
<template>
<h1 class="style">{{ address }}|{{ num.number }}</h1>
<button class="style" @click="address++">增加address的值</button>
<button class="style" @click="num.number++">增加number的值</button>
</template>
<script setup>
import { watch, reactive, ref } from "vue";
const address = ref("88");
const num = reactive({
number: 1,
name: "張三",
key: {
age: 18,
},
});
// newVal: 新值 | oldVal: 舊值
watch([address, () => num.number], (newVal, oldVal) => {
console.log(`新值:${newVal} --- 新值:${newVal}`);
console.log(`舊值:${oldVal}--- 舊值:${oldVal}`);
});
</script>
<style lang="scss" scoped>
.style {
margin: 10px 20px;
}
</style>
address的初始值為88 點(diǎn)擊左側(cè)按鈕address+1 number的值不變

number的初始值為1 點(diǎn)擊右側(cè)按鈕number+1 address的值不變

總結(jié)
到此這篇關(guān)于使用Vue3的watch實(shí)現(xiàn)數(shù)據(jù)實(shí)時(shí)更新的文章就介紹到這了,更多相關(guān)Vue3 watch數(shù)據(jù)實(shí)時(shí)更新內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實(shí)現(xiàn)一個(gè)無限加載列表功能
這篇文章主要介紹了Vue實(shí)現(xiàn)一個(gè)無限加載列表功能,需要的朋友可以參考下2018-11-11
五種Vue實(shí)現(xiàn)加減乘除運(yùn)算的方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了五種Vue實(shí)現(xiàn)加減乘除運(yùn)算的方法,文中的示例代碼簡(jiǎn)潔易懂,對(duì)我們深入了解vue有一定的幫助,需要的可以了解下2023-08-08
vue+egg+jwt實(shí)現(xiàn)登錄驗(yàn)證的示例代碼
這篇文章主要介紹了vue+egg+jwt實(shí)現(xiàn)登錄驗(yàn)證的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
el-table?選中行與復(fù)選框相互聯(lián)動(dòng)的實(shí)現(xiàn)步驟
這篇文章主要介紹了el-table?選中行與復(fù)選框相互聯(lián)動(dòng),分為兩步,第一步點(diǎn)擊行時(shí)觸發(fā)復(fù)選框的選擇或取消,第二步點(diǎn)擊復(fù)選框時(shí)觸發(fā)相應(yīng)行的變化,本文通過實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-10-10
vue中keep-alive組件實(shí)現(xiàn)多級(jí)嵌套路由的緩存
本文主要介紹了vue中keep-alive組件實(shí)現(xiàn)多級(jí)嵌套路由的緩存,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
vue中的$emit 與$on父子組件與兄弟組件的之間通信方式
本文主要對(duì)vue 用$emit 與 $on 來進(jìn)行組件之間的數(shù)據(jù)傳輸。重點(diǎn)給大家介紹vue中的$emit 與$on父子組件與兄弟組件的之間通信方式,感興趣的朋友一起看看2018-05-05
Vue項(xiàng)目中使用better-scroll實(shí)現(xiàn)一個(gè)輪播圖自動(dòng)播放功能
better-scroll是一個(gè)非常非常強(qiáng)大的第三方庫(kù) 在移動(dòng)端利用這個(gè)庫(kù) 不僅可以實(shí)現(xiàn)一個(gè)非常類似原生ScrollView的效果 也可以實(shí)現(xiàn)一個(gè)輪播圖的效果。這篇文章主要介紹了Vue項(xiàng)目中使用better-scroll實(shí)現(xiàn)一個(gè)輪播圖,需要的朋友可以參考下2018-12-12

