使用Vue3的watch實現(xiàn)數(shù)據(jù)的實時更新(附詳細代碼)
watch函數(shù)用于偵聽某個值的變化,當(dāng)該值發(fā)生改變后,觸發(fā)對應(yīng)的處理邏輯。
一、監(jiān)聽基礎(chǔ)ref類型
1.監(jiān)聽單個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 點擊按鈕后 偵聽到

2.監(jiān)聽多個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 點擊one按鈕后 偵聽到 one的值+1

點擊two按鈕后 偵聽到 two的值+1

3.監(jiān)聽一個ref對象
<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: 舊值
// 這個偵聽器無效,即控制臺無輸出
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)聽對象中的單個屬性
<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 點擊按鈕后 偵聽到

2.多層嵌套的對象
<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 點擊左側(cè)按鈕number+1 age的值不變

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

3.同時監(jiān)聽ref基本類型數(shù)據(jù)和reactive對象中的屬性
<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 點擊左側(cè)按鈕address+1 number的值不變

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

總結(jié)
到此這篇關(guān)于使用Vue3的watch實現(xiàn)數(shù)據(jù)實時更新的文章就介紹到這了,更多相關(guān)Vue3 watch數(shù)據(jù)實時更新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
el-table?選中行與復(fù)選框相互聯(lián)動的實現(xiàn)步驟
這篇文章主要介紹了el-table?選中行與復(fù)選框相互聯(lián)動,分為兩步,第一步點擊行時觸發(fā)復(fù)選框的選擇或取消,第二步點擊復(fù)選框時觸發(fā)相應(yīng)行的變化,本文通過實例代碼給大家詳細講解,需要的朋友可以參考下2022-10-10
vue中keep-alive組件實現(xiàn)多級嵌套路由的緩存
本文主要介紹了vue中keep-alive組件實現(xiàn)多級嵌套路由的緩存,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
vue中的$emit 與$on父子組件與兄弟組件的之間通信方式
本文主要對vue 用$emit 與 $on 來進行組件之間的數(shù)據(jù)傳輸。重點給大家介紹vue中的$emit 與$on父子組件與兄弟組件的之間通信方式,感興趣的朋友一起看看2018-05-05
Vue項目中使用better-scroll實現(xiàn)一個輪播圖自動播放功能
better-scroll是一個非常非常強大的第三方庫 在移動端利用這個庫 不僅可以實現(xiàn)一個非常類似原生ScrollView的效果 也可以實現(xiàn)一個輪播圖的效果。這篇文章主要介紹了Vue項目中使用better-scroll實現(xiàn)一個輪播圖,需要的朋友可以參考下2018-12-12

