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

使用Vue3的watch實現(xiàn)數(shù)據(jù)的實時更新(附詳細代碼)

 更新時間:2025年08月20日 10:10:54   作者:Derlii  
vue.js是一個輕量級的前端框架,你可以使用它來實現(xiàn)數(shù)據(jù)實時刷新,下面這篇文章主要介紹了使用Vue3的watch實現(xiàn)數(shù)據(jù)的實時更新的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下

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)文章

  • Vue實現(xiàn)一個無限加載列表功能

    Vue實現(xiàn)一個無限加載列表功能

    這篇文章主要介紹了Vue實現(xiàn)一個無限加載列表功能,需要的朋友可以參考下
    2018-11-11
  • 五種Vue實現(xiàn)加減乘除運算的方法總結(jié)

    五種Vue實現(xiàn)加減乘除運算的方法總結(jié)

    這篇文章主要為大家詳細介紹了五種Vue實現(xiàn)加減乘除運算的方法,文中的示例代碼簡潔易懂,對我們深入了解vue有一定的幫助,需要的可以了解下
    2023-08-08
  • vue+egg+jwt實現(xiàn)登錄驗證的示例代碼

    vue+egg+jwt實現(xiàn)登錄驗證的示例代碼

    這篇文章主要介紹了vue+egg+jwt實現(xiàn)登錄驗證的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • el-table?選中行與復(fù)選框相互聯(lián)動的實現(xià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)多級嵌套路由的緩存

    本文主要介紹了vue中keep-alive組件實現(xiàn)多級嵌套路由的緩存,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • vue中的$emit 與$on父子組件與兄弟組件的之間通信方式

    vue中的$emit 與$on父子組件與兄弟組件的之間通信方式

    本文主要對vue 用$emit 與 $on 來進行組件之間的數(shù)據(jù)傳輸。重點給大家介紹vue中的$emit 與$on父子組件與兄弟組件的之間通信方式,感興趣的朋友一起看看
    2018-05-05
  • vue引入jq插件的實例講解

    vue引入jq插件的實例講解

    下面小編就為大家?guī)硪黄獀ue引入jq插件的實例講解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Vue源碼分析之虛擬DOM詳解

    Vue源碼分析之虛擬DOM詳解

    所謂虛擬DOM就是為了解決瀏覽器性能問題而被設(shè)計出來的。如前,若一次操作中有 10 次更新 這篇文章主要給大家介紹了關(guān)于Vue源碼分析之虛擬DOM的相關(guān)資料,需要的朋友可以參考下
    2021-05-05
  • 淺談Vue數(shù)據(jù)綁定的原理

    淺談Vue數(shù)據(jù)綁定的原理

    本篇文章主要介紹了淺談Vue數(shù)據(jù)綁定的原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • Vue項目中使用better-scroll實現(xiàn)一個輪播圖自動播放功能

    Vue項目中使用better-scroll實現(xiàn)一個輪播圖自動播放功能

    better-scroll是一個非常非常強大的第三方庫 在移動端利用這個庫 不僅可以實現(xiàn)一個非常類似原生ScrollView的效果 也可以實現(xiàn)一個輪播圖的效果。這篇文章主要介紹了Vue項目中使用better-scroll實現(xiàn)一個輪播圖,需要的朋友可以參考下
    2018-12-12

最新評論