欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3中watch和watchEffect實(shí)戰(zhàn)梳理

 更新時(shí)間:2022年07月28日 10:58:18   作者:? 前端小大白?  
這篇文章主要介紹了vue3中watch和watchEffect實(shí)戰(zhàn)梳理,watch和watchEffect都是vue3中的監(jiān)聽(tīng)器,但是在寫(xiě)法和使用上是有區(qū)別的。下文介紹他們之間的方法及區(qū)別,需要的朋友可以參考一下

前言

watchwatchEffect都是vue3中的監(jiān)聽(tīng)器,但是在寫(xiě)法和使用上是有區(qū)別的,這篇文章主要是介紹一下watchwatchEffect的使用方法以及他們之間的區(qū)別。

watch

watch監(jiān)聽(tīng)單個(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)聽(tīng)單個(gè)數(shù)據(jù)', newVal, oldVal)
})
</script>

結(jié)果:

watch監(jiān)聽(tīng)多個(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)聽(tīng)一組數(shù)據(jù)', newVal, oldVal)
})
</script>

結(jié)果:

watch監(jiān)聽(tīng)對(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ù),deepimmediate,可以加上看看效果

watch監(jiān)聽(tīng)對(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)聽(tīng)對(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)式追蹤其依賴(lài),并在其依賴(lài)變更時(shí)重新運(yùn)行該函數(shù)。 也就是說(shuō),我們并不需要傳入一個(gè)特定的依賴(lài)源,而且它會(huì)立即執(zhí)行一遍回調(diào)函數(shù),如果函數(shù)產(chǎn)生了副作用,那它就會(huì)自動(dòng)追蹤副作用的依賴(lài)關(guān)系,自動(dòng)分析出響應(yīng)源。光看概念可能比較模糊,先來(lái)看個(gè)最簡(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, watchEffect } from 'vue'

const student = reactive({
    name: '',
    age: ''
})
watchEffect(() => {
    console.log('name: ',student.name, 'age: ', student.age)
})
</script>

結(jié)果:

watchEffect副作用

副作用,那什么是副作用呢,其實(shí)很簡(jiǎn)單,就是在監(jiān)聽(tīng)之前,我得做一件事。

<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)聽(tīng)之前讓student.name賦值為'張三',無(wú)論你輸入什么值,name一直都是'張三'

停止監(jiān)聽(tīng)

我們用同步語(yǔ)句創(chuàng)建的監(jiān)聽(tīng)器,會(huì)自動(dòng)綁定到組件實(shí)例上,并且會(huì)在組件卸載時(shí)自動(dòng)停止,但是,如果我們?cè)诋惒交卣{(diào)里創(chuàng)建一個(gè)監(jiān)聽(tīng)器,那它就不會(huì)綁定到當(dāng)前組件上,必須手動(dòng)去停止,防止內(nèi)存泄漏。 那怎么去停止呢,其實(shí)我們只需要調(diào)用一下watchwatchEffect返回的函數(shù)

const stop = watchEffect(() => {})

// 停止監(jiān)聽(tīng)
unwatch()

區(qū)別

用了一遍watchwatchEffect之后,發(fā)現(xiàn)他倆主要有以下幾點(diǎn)區(qū)別:

  • watch是惰性執(zhí)行的,而watchEffect不是,不考慮watch第三個(gè)配置參數(shù)的情況下,watch在組件第一次執(zhí)行的時(shí)候是不會(huì)執(zhí)行的,只有在之后依賴(lài)項(xiàng)變化的時(shí)候再執(zhí)行,而watchEffect是在程序執(zhí)行到此處的時(shí)候就會(huì)立即執(zhí)行,而后再響應(yīng)其依賴(lài)變化執(zhí)行。
  • watch需要傳遞監(jiān)聽(tīng)的對(duì)象,watchEffect不需要

到此這篇關(guān)于vue3中watch和watchEffect實(shí)戰(zhàn)梳理的文章就介紹到這了,更多相關(guān)vue watch和watchEffect 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論