vue3中watch和watchEffect實(shí)戰(zhàn)梳理
前言
watch和watchEffect都是vue3中的監(jiān)聽器,但是在寫法和使用上是有區(qū)別的,這篇文章主要是介紹一下watch和watchEffect的使用方法以及他們之間的區(qū)別。
watch
watch監(jiān)聽單個(gè)數(shù)據(jù)
<template>
<input type="text" v-model="text1" />
</template>
<script setup>
import { ref, watch } from 'vue'
const text1 = ref('')
watch(text1, (newVal, oldVal) => {
console.log('監(jiān)聽單個(gè)數(shù)據(jù)', newVal, oldVal)
})
</script>結(jié)果:

watch監(jiān)聽多個(gè)數(shù)據(jù)
<template>
<input type="text" v-model="text1" />
<input type="text" v-model="text2" />
</template>
<script setup>
import { ref, watch } from 'vue'
const text1 = ref('')
const text2 = ref('')
watch([text1, text2], (newVal, oldVal) => {
console.log('監(jiān)聽一組數(shù)據(jù)', newVal, oldVal)
})
</script>結(jié)果:

watch監(jiān)聽對(duì)象
<template>
name: <input type="text" v-model="student.name" />
age: <input type="number" v-model="student.age" />
</template>
<script setup>
import { reactive, watch } from 'vue'
const student = reactive({
name: '',
age: ''
})
watch(student, (newVal, oldVal) => {
console.log('newVal', newVal)
console.log('oldVal', newVal)
})
</script>結(jié)果:

watch還有第三個(gè)參數(shù),deep和immediate,可以加上看看效果
watch監(jiān)聽對(duì)象的某一個(gè)值
<template>
name: <input type="text" v-model="student.name" />
age: <input type="number" v-model="student.age" />
</template>
<script lang="ts" setup>
import { reactive, watch } from 'vue'
const student = reactive({
name: '',
age: ''
})
watch(() => student.name, (newVal, oldVal) => {
console.log('newVal', newVal)
console.log('oldVal', newVal)
}, {
deep: true,
immediate: true
})
</script>監(jiān)聽對(duì)象某一個(gè)屬性的時(shí)候需要用箭頭函數(shù) 結(jié)果:

watchEffect
關(guān)于watchEffect,官網(wǎng)是這么介紹的:為了根據(jù)響應(yīng)式狀態(tài)自動(dòng)應(yīng)用和重新應(yīng)用副作用,我們可以使用watchEffect方法,它立即執(zhí)行傳入的一個(gè)函數(shù),同時(shí)響應(yīng)式追蹤其依賴,并在其依賴變更時(shí)重新運(yùn)行該函數(shù)。 也就是說,我們并不需要傳入一個(gè)特定的依賴源,而且它會(huì)立即執(zhí)行一遍回調(diào)函數(shù),如果函數(shù)產(chǎn)生了副作用,那它就會(huì)自動(dòng)追蹤副作用的依賴關(guān)系,自動(dòng)分析出響應(yīng)源。光看概念可能比較模糊,先來看個(gè)最簡單的例子:
<template>
name: <input type="text" v-model="student.name" />
age: <input type="number" v-model="student.age" />
</template>
<script lang="ts" setup>
import { reactive, watchEffect } from 'vue'
const student = reactive({
name: '',
age: ''
})
watchEffect(() => {
console.log('name: ',student.name, 'age: ', student.age)
})
</script>結(jié)果:

watchEffect副作用
副作用,那什么是副作用呢,其實(shí)很簡單,就是在監(jiān)聽之前,我得做一件事。
<template>
name: <input type="text" v-model="student.name" />
age: <input type="number" v-model="student.age" />
<h2>{{student.name}}</h2>
</template>
<script lang="ts" setup>
import { reactive, watchEffect } from 'vue'
const student = reactive({
name: '',
age: ''
})
watchEffect((oninvalidate) => {
oninvalidate(() => {
student.name = '張三'
})
console.log('name: ', student.name)
}, {
// pre 組件更新前; sync:強(qiáng)制效果始終同步; post:組件更新后執(zhí)行
flush: 'post' // dom加載完畢后執(zhí)行
})
</script>監(jiān)聽之前讓student.name賦值為'張三',無論你輸入什么值,name一直都是'張三'

停止監(jiān)聽
我們用同步語句創(chuàng)建的監(jiān)聽器,會(huì)自動(dòng)綁定到組件實(shí)例上,并且會(huì)在組件卸載時(shí)自動(dòng)停止,但是,如果我們?cè)诋惒交卣{(diào)里創(chuàng)建一個(gè)監(jiān)聽器,那它就不會(huì)綁定到當(dāng)前組件上,必須手動(dòng)去停止,防止內(nèi)存泄漏。 那怎么去停止呢,其實(shí)我們只需要調(diào)用一下watch或watchEffect返回的函數(shù)
const stop = watchEffect(() => {})
// 停止監(jiān)聽
unwatch()區(qū)別
用了一遍watch和watchEffect之后,發(fā)現(xiàn)他倆主要有以下幾點(diǎn)區(qū)別:
watch是惰性執(zhí)行的,而watchEffect不是,不考慮watch第三個(gè)配置參數(shù)的情況下,watch在組件第一次執(zhí)行的時(shí)候是不會(huì)執(zhí)行的,只有在之后依賴項(xiàng)變化的時(shí)候再執(zhí)行,而watchEffect是在程序執(zhí)行到此處的時(shí)候就會(huì)立即執(zhí)行,而后再響應(yīng)其依賴變化執(zhí)行。watch需要傳遞監(jiān)聽的對(duì)象,watchEffect不需要
到此這篇關(guān)于vue3中watch和watchEffect實(shí)戰(zhàn)梳理的文章就介紹到這了,更多相關(guān)vue watch和watchEffect 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解Vue3?中的watchEffect?特性
- vue3數(shù)據(jù)監(jiān)聽watch/watchEffect的示例代碼
- vue3中watch與watchEffect的區(qū)別
- Vue3中的?computed,watch,watchEffect的使用方法
- Vue3?中?watch?與?watchEffect?區(qū)別及用法小結(jié)
- vue3中的watch和watchEffect實(shí)例詳解
- 淺談Vue3中watchEffect的具體用法
- vue3的watch和watchEffect你了解嗎
- VUE3中watch和watchEffect的用法詳解
- Vue3中watchEffect的用途淺析
- vue3中watch和watchEffect的區(qū)別
相關(guān)文章
使用vue-cli創(chuàng)建vue項(xiàng)目介紹
這篇文章介紹了使用vue-cli創(chuàng)建vue項(xiàng)目的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01
vue如何基于el-table實(shí)現(xiàn)多頁多選及翻頁回顯過程
在最近的一個(gè)項(xiàng)目中我需要實(shí)現(xiàn)表格的翻頁,并且還要實(shí)現(xiàn)全選、多選功能,下面這篇文章主要給大家介紹了關(guān)于vue如何基于el-table實(shí)現(xiàn)多頁多選及翻頁回顯過程的相關(guān)資料,需要的朋友可以參考下2022-12-12
vue父元素點(diǎn)擊事件與子元素點(diǎn)擊事件沖突問題
這篇文章主要介紹了vue父元素點(diǎn)擊事件與子元素點(diǎn)擊事件沖突問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Vue3響應(yīng)式對(duì)象是如何實(shí)現(xiàn)的(2)
這篇文章主要介紹了Vue3響應(yīng)式對(duì)象是如何實(shí)現(xiàn)的,文章基于上篇文章展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
VSCode搭建vue項(xiàng)目的實(shí)現(xiàn)步驟
本文主要介紹了VSCode搭建vue項(xiàng)目的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
解決Vue.js由于延時(shí)顯示了{(lán){message}}引用界面的問題
今天小編就為大家分享一篇解決Vue.js由于延時(shí)顯示了{(lán){message}}引用界面的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
在axios中使用params傳參的時(shí)候傳入數(shù)組的方法
今天小編就為大家分享一篇在axios中使用params傳參的時(shí)候傳入數(shù)組的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09

