最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用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ù)用于偵聽某個值的變化,當該值發(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)文章

  • React之input動態(tài)取值和賦值方式

    React之input動態(tài)取值和賦值方式

    這篇文章主要介紹了React之input動態(tài)取值和賦值方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Vue3連接SSE并且返回結(jié)果用打字機效果呈現(xiàn)方法

    Vue3連接SSE并且返回結(jié)果用打字機效果呈現(xiàn)方法

    SSE的核心是一個持久的HTTP連接,服務(wù)端通過該連接不斷發(fā)送文本格式的數(shù)據(jù),下面這篇文章主要介紹了Vue3連接SSE并且返回結(jié)果用打字機效果呈現(xiàn)方法的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2026-03-03
  • vue3源碼分析reactivity實現(xiàn)原理

    vue3源碼分析reactivity實現(xiàn)原理

    這篇文章主要為大家介紹了vue3源碼分析reactivity實現(xiàn)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • 詳解Vue This$Store總結(jié)

    詳解Vue This$Store總結(jié)

    這篇文章主要介紹了詳解Vue This$Store總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • Vue實現(xiàn)路由過渡動效的4種方法

    Vue實現(xiàn)路由過渡動效的4種方法

    Vue 路由過渡是對 Vue 程序一種快速簡便的增加個性化效果的的方法,這篇文章主要介紹了Vue實現(xiàn)路由過渡動效的4種方法,感興趣的可以了解一下
    2021-05-05
  • vue通過watch對input做字數(shù)限定的方法

    vue通過watch對input做字數(shù)限定的方法

    本篇文章主要介紹了vue通過watch對input做字數(shù)限定的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • vue配置路徑別名方式(@/*)

    vue配置路徑別名方式(@/*)

    文章介紹了如何配置路徑別名,包括安裝@types/node、編輯vite.config.ts或js文件、配置tsconfig.json以及重啟軟件等步驟
    2025-11-11
  • vue 利用路由守衛(wèi)判斷是否登錄的方法

    vue 利用路由守衛(wèi)判斷是否登錄的方法

    今天小編就為大家分享一篇vue 利用路由守衛(wèi)判斷是否登錄的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • el-table樹形表格表單驗證(列表生成序號)

    el-table樹形表格表單驗證(列表生成序號)

    這篇文章主要介紹了el-table樹形表格表單驗證(列表生成序號),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Vue3+Vant實現(xiàn)簡單的登錄功能

    Vue3+Vant實現(xiàn)簡單的登錄功能

    這篇文章主要為大家詳細介紹了Vue3如何結(jié)合Vant實現(xiàn)簡單的登錄功能,文中的示例代碼講解詳細,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-04-04

最新評論

天水市| 弋阳县| 霍山县| 大洼县| 林西县| 皋兰县| 汉中市| 连州市| 建德市| 秦皇岛市| 册亨县| 遂溪县| 来安县| 定兴县| 都安| 仙游县| 文化| 渑池县| 鄯善县| 从江县| 南部县| 抚松县| 宜宾县| 连平县| 织金县| 嘉义市| 上犹县| 南岸区| 乌拉特中旗| 扶沟县| 阜城县| 白城市| 彭水| 长岭县| 石台县| 确山县| 衡南县| 阿拉善左旗| 南乐县| 台湾省| 长沙市|