VUE3中watch和watchEffect的用法詳解
watch和watchEffect都是監(jiān)聽器,但在寫法和使用上有所區(qū)別。
watch在監(jiān)聽 ref 類型時和監(jiān)聽reactive類型時watch函數(shù)的寫發(fā)有所不一樣。
watch在監(jiān)聽 ref 類型時:
<script> import {ref, watch} from 'vue' export default { ? ? setup() {? ? ? ? ? const state = ref(0) ? ? ? ? watch(state, (newValue, oldValue) => { ? ? ? ? ? console.log(`原值為${oldValue}`) ? ? ? ? ? console.log(`新值為${newValue}`) ? ? ? ? ? /* 1秒后打印結(jié)果: ? ? ? ? ? ? ? ? ? 原值為0 ? ? ? ? ? ? ? ? ? 新值為1 ? ? ? ? ? */ ? ? ? ? }) ? ? ? ? // 1秒后將state值+1 ? ? ? ? setTimeout(() => { ? ? ? ? ? state.value ++ ? ? ? ? }, 1000) ? ? } } </script>
watch在監(jiān)聽 reactive類型時:
<script> import {reactive, watch} from 'vue' export default { ? ? setup() {? ? ? ? ? const state = reactive({count: 0}) ? ? ? ? watch(() => state.count, (newValue, oldValue) => { ? ? ? ? ? console.log(`原值為${oldValue}`) ? ? ? ? ? console.log(`新值為${newValue}`) ? ? ? ? ? /* 1秒后打印結(jié)果: ? ? ? ? ? ? ? ? ? 原值為0 ? ? ? ? ? ? ? ? ? 新值為1 ? ? ? ? ? */ ? ? ? ? }) ? ? ? ? // 1秒后將state.count的值+1 ? ? ? ? setTimeout(() => { ? ? ? ? ? state.count ++ ? ? ? ? }, 1000) ? ? } } </script>
watchEffect 它與 watch 的區(qū)別主要有以下幾點(diǎn):
- 不需要手動傳入依賴
- 每次初始化時會執(zhí)行一次回調(diào)函數(shù)來自動獲取依賴
- 無法獲取到原值,只能得到變化后的值
<script> import {reactive, watchEffect} from 'vue' export default { ? ? setup() {? ? ? ? ? ? const state = reactive({ count: 0, name: 'zs' }) ? ? ? ? ? watchEffect(() => { ? ? ? ? ? console.log(state.count) ? ? ? ? ? console.log(state.name) ? ? ? ? ? /* ?初始化時打印: ? ? ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? ? ? zs ? ? ? ? ? ? 1秒后打?。? ? ? ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ls ? ? ? ? ? */ ? ? ? ? ? }) ? ? ? ? ? setTimeout(() => { ? ? ? ? ? ? state.count ++ ? ? ? ? ? ? state.name = 'ls' ? ? ? ? ? }, 1000) ? ? } } </script>
根據(jù)以上特征,我們可以自行選擇使用哪一個監(jiān)聽器
另:watch和watchEffect監(jiān)聽器在同一個頁面中都可以使用多次,對于分別監(jiān)聽多個變量的時候
到此這篇關(guān)于VUE3中watch和watchEffect的用法詳解的文章就介紹到這了,更多相關(guān)VUE3 watch和watchEffect內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解Vue3?中的watchEffect?特性
- vue3數(shù)據(jù)監(jiān)聽watch/watchEffect的示例代碼
- vue3中watch與watchEffect的區(qū)別
- vue3中watch和watchEffect實(shí)戰(zhàn)梳理
- Vue3中的?computed,watch,watchEffect的使用方法
- Vue3?中?watch?與?watchEffect?區(qū)別及用法小結(jié)
- vue3中的watch和watchEffect實(shí)例詳解
- 淺談Vue3中watchEffect的具體用法
- vue3的watch和watchEffect你了解嗎
- Vue3中watchEffect的用途淺析
- vue3中watch和watchEffect的區(qū)別
相關(guān)文章
vue中radio單選框如何實(shí)現(xiàn)取消選中狀態(tài)問題
這篇文章主要介紹了vue中radio單選框如何實(shí)現(xiàn)取消選中狀態(tài)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05淺談實(shí)現(xiàn)在線預(yù)覽PDF的幾種解決辦法
這篇文章主要介紹了淺談實(shí)現(xiàn)在線預(yù)覽PDF的幾種解決辦法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08基于vue+echarts數(shù)據(jù)可視化大屏展示的實(shí)現(xiàn)
這篇文章主要介紹了基于vue+echarts數(shù)據(jù)可視化大屏展示的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Vue實(shí)現(xiàn)數(shù)據(jù)請求攔截
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)數(shù)據(jù)請求攔截,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10Vue.extend 編程式插入組件的實(shí)現(xiàn)
這篇文章主要介紹了Vue.extend 編程式插入組件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11關(guān)于Vue?"__ob__:Observer"屬性的解決方案詳析
在操作數(shù)據(jù)的時候發(fā)現(xiàn),__ob__: Observer這個屬性出現(xiàn)之后,如果單獨(dú)拿數(shù)據(jù)的值,就會返回undefined,下面這篇文章主要給大家介紹了關(guān)于Vue?"__ob__:Observer"屬性的解決方案,需要的朋友可以參考下2022-11-11