詳解Vue3中的watch偵聽(tīng)器和watchEffect高級(jí)偵聽(tīng)器
1watch偵聽(tīng)器
watch 需要偵聽(tīng)特定的數(shù)據(jù)源,并在單獨(dú)的回調(diào)函數(shù)中執(zhí)行副作用
- watch第一個(gè)參數(shù)監(jiān)聽(tīng)源
- watch第二個(gè)參數(shù)回調(diào)函數(shù)cb(newVal,oldVal)
- watch第三個(gè)參數(shù)一個(gè)options配置項(xiàng)是一個(gè)對(duì)
- {
- immediate:true //是否立即調(diào)用一次
- deep:true //是否開(kāi)啟深度監(jiān)聽(tīng)
- }
監(jiān)聽(tīng)Ref 案例:
import { ref, watch } from 'vue' let message = ref({ nav:{ bar:{ name:"" } } }) watch(message, (newVal, oldVal) => { console.log('新的值----', newVal); console.log('舊的值----', oldVal); },{ immediate:true, deep:true
監(jiān)聽(tīng)多個(gè)ref 注意變成數(shù)組:
import { ref, watch ,reactive} from 'vue' let message = ref('') let message2 = ref('') watch([message,message2], (newVal, oldVal) => { console.log('新的值----', newVal); console.log('舊的值----', oldVal); })
監(jiān)聽(tīng)Reactive:使用reactive監(jiān)聽(tīng)深層對(duì)象開(kāi)啟和不開(kāi)啟deep 效果一樣
import { ref, watch ,reactive} from 'vue' let message = reactive({ nav:{ bar:{ name:"" } } }) watch(message, (newVal, oldVal) => { console.log('新的值----', newVal); console.log('舊的值----', oldVal); })
監(jiān)聽(tīng)reactive 單一值
import { ref, watch ,reactive} from 'vue' let message = reactive({ name:"", name2:"" }) watch(()=>message.name, (newVal, oldVal) => { console.log('新的值----', newVal); console.log('舊的值----', oldVal); })
2watchEffect高級(jí)偵聽(tīng)器
立即執(zhí)行傳入的一個(gè)函數(shù),同時(shí)響應(yīng)式追蹤其依賴(lài),并在其依賴(lài)變更時(shí)重新運(yùn)行該函數(shù)。如果用到message 就只會(huì)監(jiān)聽(tīng)message 就是用到幾個(gè)監(jiān)聽(tīng)?zhēng)讉€(gè) 而且是非惰性 會(huì)默認(rèn)調(diào)用一次。
let message = ref<string>('') let message2 = ref<string>('') watchEffect(() => { //console.log('message', message.value); console.log('message2', message2.value); })
清除副作用:就是在觸發(fā)監(jiān)聽(tīng)之前會(huì)調(diào)用一個(gè)函數(shù)可以處理你的邏輯例如防抖
import { watchEffect, ref } from 'vue' let message = ref<string>('') let message2 = ref<string>('') watchEffect((oninvalidate) => { //console.log('message', message.value); oninvalidate(()=>{ }) console.log('message2', message2.value); })
停止跟蹤 watchEffect 返回一個(gè)函數(shù) 調(diào)用之后將停止更新
const stop = watchEffect((oninvalidate) => { //console.log('message', message.value); oninvalidate(()=>{ }) console.log('message2', message2.value); },{ flush:"post",// pre:組件更新前執(zhí)行;sync:強(qiáng)制效果始終同步觸發(fā),post:組件更新后執(zhí)行 onTrigger () { //onTrigger 可以幫助我們調(diào)試 watchEffect } }) stop()
到此這篇關(guān)于Vue3中的watch偵聽(tīng)器和watchEffect高級(jí)偵聽(tīng)器的文章就介紹到這了,更多相關(guān)Vue3watch偵聽(tīng)器和watchEffect偵聽(tīng)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3處理錯(cuò)誤邊界(error boundaries)的示例代碼
在開(kāi)發(fā) Vue 3 應(yīng)用時(shí),處理錯(cuò)誤邊界(Error Boundaries)是一個(gè)重要的考量,在 Vue 3 中實(shí)現(xiàn)錯(cuò)誤邊界的方式與 React 等其他框架有所不同,下面,我們將深入探討 Vue 3 中如何實(shí)現(xiàn)錯(cuò)誤邊界,并提供一些示例代碼幫助理解什么是錯(cuò)誤邊界,需要的朋友可以參考下2024-10-10electron-vue中報(bào)錯(cuò)Cannot?use?import?statement?outside?a?m
Electron 是一個(gè)使用 JavaScript、HTML 和 CSS 構(gòu)建桌面應(yīng)用程序的框架,下面這篇文章主要給大家介紹了關(guān)于electron-vue中報(bào)錯(cuò)Cannot?use?import?statement?outside?a?module的解決方案,需要的朋友可以參考下2023-02-02vue組件開(kāi)發(fā)props驗(yàn)證的實(shí)現(xiàn)
這篇文章主要介紹了vue組件開(kāi)發(fā)props驗(yàn)證的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02Vue2.0利用vue-resource上傳文件到七牛的實(shí)例代碼
本篇文章主要介紹了Vue2.0利用vue-resource上傳文件到七牛的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07在vue中import()語(yǔ)法不能傳入變量的問(wèn)題及解決
這篇文章主要介紹了在vue中import()語(yǔ)法不能傳入變量的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04vue實(shí)現(xiàn)自動(dòng)滑動(dòng)輪播圖片
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)自動(dòng)滑動(dòng)輪播圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Vue2+SpringBoot實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出到csv文件并下載的使用示例
本文主要介紹了Vue2+SpringBoot實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出到csv文件并下載,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-10-10Vue上傳組件vue Simple Uploader的用法示例
本篇文章主要介紹了Vue上傳組件vue Simple Uploader的用法示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-08-08Vue3中使用pnpm搭建monorepo開(kāi)發(fā)環(huán)境
這篇文章主要為大家介紹了Vue3中使用pnpm搭建monorepo開(kāi)發(fā)環(huán)境示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08