vue3中watch和watchEffect實戰(zhàn)梳理
前言
watch和watchEffect都是vue3中的監(jiān)聽器,但是在寫法和使用上是有區(qū)別的,這篇文章主要是介紹一下watch和watchEffect的使用方法以及他們之間的區(qū)別。
watch
watch監(jiān)聽單個數(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)聽單個數(shù)據(jù)', newVal, oldVal)
})
</script>結(jié)果:

watch監(jiān)聽多個數(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)聽對象
<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還有第三個參數(shù),deep和immediate,可以加上看看效果
watch監(jiān)聽對象的某一個值
<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)聽對象某一個屬性的時候需要用箭頭函數(shù) 結(jié)果:

watchEffect
關(guān)于watchEffect,官網(wǎng)是這么介紹的:為了根據(jù)響應(yīng)式狀態(tài)自動應(yīng)用和重新應(yīng)用副作用,我們可以使用watchEffect方法,它立即執(zhí)行傳入的一個函數(shù),同時響應(yīng)式追蹤其依賴,并在其依賴變更時重新運行該函數(shù)。 也就是說,我們并不需要傳入一個特定的依賴源,而且它會立即執(zhí)行一遍回調(diào)函數(shù),如果函數(shù)產(chǎn)生了副作用,那它就會自動追蹤副作用的依賴關(guān)系,自動分析出響應(yīng)源。光看概念可能比較模糊,先來看個最簡單的例子:
<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副作用
副作用,那什么是副作用呢,其實很簡單,就是在監(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:強制效果始終同步; post:組件更新后執(zhí)行
flush: 'post' // dom加載完畢后執(zhí)行
})
</script>監(jiān)聽之前讓student.name賦值為'張三',無論你輸入什么值,name一直都是'張三'

停止監(jiān)聽
我們用同步語句創(chuàng)建的監(jiān)聽器,會自動綁定到組件實例上,并且會在組件卸載時自動停止,但是,如果我們在異步回調(diào)里創(chuàng)建一個監(jiān)聽器,那它就不會綁定到當前組件上,必須手動去停止,防止內(nèi)存泄漏。 那怎么去停止呢,其實我們只需要調(diào)用一下watch或watchEffect返回的函數(shù)
const stop = watchEffect(() => {})
// 停止監(jiān)聽
unwatch()區(qū)別
用了一遍watch和watchEffect之后,發(fā)現(xiàn)他倆主要有以下幾點區(qū)別:
watch是惰性執(zhí)行的,而watchEffect不是,不考慮watch第三個配置參數(shù)的情況下,watch在組件第一次執(zhí)行的時候是不會執(zhí)行的,只有在之后依賴項變化的時候再執(zhí)行,而watchEffect是在程序執(zhí)行到此處的時候就會立即執(zhí)行,而后再響應(yīng)其依賴變化執(zhí)行。watch需要傳遞監(jiān)聽的對象,watchEffect不需要
到此這篇關(guān)于vue3中watch和watchEffect實戰(zhàn)梳理的文章就介紹到這了,更多相關(guān)vue watch和watchEffect 內(nèi)容請搜索腳本之家以前的文章或繼續(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實例詳解
- 淺談Vue3中watchEffect的具體用法
- vue3的watch和watchEffect你了解嗎
- VUE3中watch和watchEffect的用法詳解
- Vue3中watchEffect的用途淺析
- vue3中watch和watchEffect的區(qū)別
相關(guān)文章
Avue自定義formslot調(diào)用rules自定義規(guī)則方式
在Avue框架中,使用formslot自定義表格列時可能會遇到無法調(diào)用Avue的自定義校驗規(guī)則的問題,這通常發(fā)生在嘗試通過formslot自定義設(shè)置列的場景中,解決這一問題的一個有效方法是將自定義列與Avue的校驗規(guī)則通過特定方式連接起來2024-10-10

