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

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

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

watch函數(shù)用于偵聽某個(gè)值的變化,當(dāng)該值發(fā)生改變后,觸發(fā)對應(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對象

<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è)偵聽器無效,即控制臺無輸出
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)聽對象中的單個(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.多層嵌套的對象

<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對象中的屬性

<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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

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

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

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

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

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

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

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

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

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

    vue通過watch對input做字?jǐn)?shù)限定的方法

    本篇文章主要介紹了vue通過watch對input做字?jǐn)?shù)限定的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    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)判斷是否登錄的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • el-table樹形表格表單驗(yàn)證(列表生成序號)

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

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

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

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

最新評論

禄劝| 咸宁市| 巨野县| 嘉黎县| 昭平县| 陆川县| 介休市| 湾仔区| 聂拉木县| 忻城县| 和顺县| 新巴尔虎左旗| 镇平县| 苏尼特左旗| 平邑县| 仁怀市| 葫芦岛市| 洛川县| 甘德县| 汤原县| 海丰县| 甘孜县| 阿瓦提县| 乌什县| 克什克腾旗| 长白| 前郭尔| 龙陵县| 特克斯县| 微博| 岗巴县| 寿宁县| 杭锦后旗| 聂荣县| 丹东市| 壤塘县| 宽甸| 葫芦岛市| 馆陶县| 建瓯市| 洛川县|