Vue3中watchEffect高級(jí)偵聽(tīng)器的具體使用
示例均采用 <script setup>,且包含 typescript 的基礎(chǔ)用法
前言
Vue3 中新增了一種特殊的監(jiān)聽(tīng)器 watchEffect,它的類(lèi)型是:
function watchEffect( effect: (onCleanup: OnCleanup) => void, options?: WatchEffectOptions ): StopHandle
下面通過(guò)實(shí)例來(lái)理解下它的用法
一、簡(jiǎn)單使用
- 第一個(gè)參數(shù)就是要運(yùn)行的
副作用函數(shù) effect - 函數(shù)內(nèi)
用到哪些數(shù)據(jù)才會(huì)監(jiān)聽(tīng)哪些數(shù)據(jù) - 且會(huì)
立刻執(zhí)行一次(immediate)
<input type="text" v-model="message1" /> <br /> <input type="text" v-model="message2" /> <br />
import { ref, watchEffect } from "vue";
const message1 = ref<string>("飛機(jī)");
const message2 = ref<string>("火車(chē)");
watchEffect(() => {
console.log("message1========>,", message1);
// 不使用 message2 就不會(huì)監(jiān)聽(tīng) message2
// console.log("message2========>,", message2);
});
二、副作用 effect 的參數(shù)
- effect 的參數(shù) 也是一個(gè)
函數(shù),用來(lái)注冊(cè)清理回調(diào)。 清理回調(diào)會(huì)在該副作用下一次執(zhí)行前被調(diào)用,可以用來(lái)清理無(wú)效的副作用,例如等待中的異步請(qǐng)求
<input type="text" v-model="message1" /> <br /> <input type="text" v-model="message2" /> <br />
import { ref, watchEffect } from "vue";
const message1 = ref<string>("飛機(jī)");
const message2 = ref<string>("火車(chē)");
watchEffect((onCleanup) => {
console.log("message11111========>,", message1);
console.log("message22222========>,", message2);
onCleanup(() => {
console.log("onCleanup —————— 下一次運(yùn)行之前要做的事");
});
});
頁(yè)面刷新,首次打?。?/h3>

更改輸入框的值,再次打?。?/h3>

三、watchEffect 返回值
- 返回值是一個(gè)用來(lái)
停止偵聽(tīng)器的函數(shù),調(diào)用后不再偵聽(tīng) - 需要注意的是:停止時(shí),
不影響最后一次 onCleanup 的執(zhí)行
<input type="text" v-model="message1" /> <br /> <input type="text" v-model="message2" /> <br /> <button @click="stopWatch">停止watchEffect</button>
const stop = watchEffect((onCleanup) => {
console.log("message11111========>,", message1);
console.log("message22222========>,", message2);
onCleanup(() => {
console.log("onCleanup —————— 下一次運(yùn)行之前要做的事");
});
});
const stopWatch = () => {
stop();
};
頁(yè)面刷新,首次打?。?/h3>

更改輸入框的值,再次打?。?/h3>

點(diǎn)擊按鈕 停止偵聽(tīng),再次打印:

四、options配置
watchEffect 的第二個(gè)參數(shù)是配置項(xiàng):
flush:watch 的執(zhí)行順序pre|post|sync,默認(rèn):pre,具體含義可以看上一篇 watch 中的解釋- 一般需要在 dom 更新之后再獲取的情況,可以設(shè)置為
post
onTrack用于開(kāi)發(fā)環(huán)境調(diào)試onTrigger用于開(kāi)發(fā)環(huán)境調(diào)試
<input id="ipt" v-model="iptVal" />
const iptVal = ref<string>("aa");
watchEffect(
() => {
// 測(cè)試 flush
const spanEle = document.getElementById("ipt");
// flush = pre 時(shí),打印 null; flush = post 時(shí),打印節(jié)點(diǎn)
console.log("spanEle========>,", spanEle);
// 修改 iptVal 測(cè)試 onTrack、onTrigger
console.log("iptVal============>", iptVal.value);
},
{
flush: "post",
// 收集依賴(lài)時(shí)觸發(fā)
onTrack: () => {
debugger;
},
// 更新時(shí)觸發(fā)
onTrigger: () => {
debugger;
},
}
);
五、周邊 api
watchPostEffect():watchEffect()使用flush: 'post'選項(xiàng)時(shí)的別名watchSyncEffect():watchEffect()使用flush: 'sync'選項(xiàng)時(shí)的別名
到此這篇關(guān)于Vue3中watchEffect高級(jí)偵聽(tīng)器的具體使用的文章就介紹到這了,更多相關(guān)Vue3 watchEffect高級(jí)偵聽(tīng)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue3中Watch、Watcheffect、Computed的使用和區(qū)別解析
- vue3 watch和watchEffect的使用以及有哪些區(qū)別
- Vue3.0監(jiān)聽(tīng)器watch與watchEffect詳解
- vue3中的watch和watchEffect實(shí)例詳解
- 淺談Vue3中watchEffect的具體用法
- Vue3?中?watch?與?watchEffect?區(qū)別及用法小結(jié)
- VUE3中watch和watchEffect的用法詳解
- 一文搞懂Vue3中watchEffect偵聽(tīng)器的使用
- Vue3中watchEffect和watch的基礎(chǔ)應(yīng)用詳解
- Vue3中watch與watchEffect使用方法詳解
相關(guān)文章
前端XSS攻擊場(chǎng)景詳解與Vue.js處理XSS的方法(vue-xss)
這篇文章主要給大家介紹了關(guān)于前端XSS攻擊場(chǎng)景與Vue.js使用vue-xss處理XSS的方法,介紹了實(shí)際工作中渲染數(shù)據(jù)時(shí)遇到XSS攻擊時(shí)的防范措施,以及解決方案,需要的朋友可以參考下2024-02-02
vue 2.0 購(gòu)物車(chē)小球拋物線(xiàn)的示例代碼
本篇文章主要介紹了vue 2.0 購(gòu)物車(chē)小球拋物線(xiàn)的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
詳解如何實(shí)現(xiàn)Vue組件的動(dòng)態(tài)綁定
Vue.js 是一個(gè)漸進(jìn)式框架,用于構(gòu)建用戶(hù)界面,在開(kāi)發(fā)過(guò)程中,我們經(jīng)常需要根據(jù)不同的條件動(dòng)態(tài)顯示組件,在本文中,我將詳細(xì)介紹如何實(shí)現(xiàn)Vue組件的動(dòng)態(tài)綁定,提供示例代碼,以幫助你更深入地理解這個(gè)概念,需要的朋友可以參考下2024-11-11
關(guān)于element el-input的autofocus失效的問(wèn)題及解決
這篇文章主要介紹了關(guān)于element el-input的autofocus失效的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
解決Ant Design Modal內(nèi)嵌Form表單initialValue值不動(dòng)態(tài)更新問(wèn)題
這篇文章主要介紹了解決Ant Design Modal內(nèi)嵌Form表單initialValue值不動(dòng)態(tài)更新問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10

