vue3 中watch和watchEffect

监听:首先你要知道监听的是谁

1、下边是监听search变量的例子,后边的函数newSearch和prevSearch这两个形参,当然名字可以自己定义,newSearch是新值,prevSearch是原先的值,如下

<script setup>
import { ref, reactive, computed,watch,watchEffect } from "vue";
const names = ref([{ name: "小猫" }, { name: "小狗" }, { name: "小猪" }]);
const search = ref("");
const searchNames = computed(() => {
    return names.value.filter((item) => item.name.includes(search.value));
})
watch(search,(newSearch,prevSearch)=>{
    console.log("watch函数触发了",newSearch,prevSearch);
})
</script>

<template>
    <div class="home">
        <!--input textarea select 这三个标签支持双向数据绑定-->
        <input type="text" v-model="search" />
        <p v-for="(item,index) in searchNames" :key="index">{{item.name}}</p>
    </div>
</template>

<style scoped>

</style>

 2、下边监听的是多个对象,用数组的形式,如下

<script setup>
import { ref, reactive, computed,watch,watchEffect } from "vue";
const names = ref([{ name: "小猫" }, { name: "小狗" }, { name: "小猪" }]);
const search = ref("");
const searchNames = computed(() => {
    return names.value.filter((item) => item.name.includes(search.value));
})
watch([search,names],([newSearch,newNames],[prevSearch,prevNames])=>{
    console.log("watch函数触发了",newSearch,prevSearch,newNames,prevNames);
})
</script>

<template>
    <div class="home">
        <!--input textarea select 这三个标签支持双向数据绑定-->
        <input type="text" v-model="search" />
        <p v-for="(item,index) in searchNames" :key="index">{{item.name}}</p>
    </div>
</template>

<style scoped>

</style>

watch([search,names],([newSearch,newNames],[prevSearch,prevNames])=>{
    console.log("watch函数触发了",newSearch,prevSearch,newNames,prevNames);
})

监听的对象和后边的函数中的参数是对应的关系。

3、watchEffect:默认初始化会执行一次 ,和watch不同的是你不需要指定监听某个属性,而是在回调函数中应用了某个属性,只要这个属性发生变化就会监听执行。

<script setup>
import { ref, reactive, computed,watch,watchEffect } from "vue";
const names = ref([{ name: "小猫" }, { name: "小狗" }, { name: "小猪" }]);
const search = ref("");
const searchNames = computed(() => {
    return names.value.filter((item) => item.name.includes(search.value));
})
watch([search,names],([newSearch,newNames],[prevSearch,prevNames])=>{
    console.log("watch函数触发了",newSearch,prevSearch,newNames,prevNames);
})
watchEffect(()=>{
    console.log("watchEffect触发了"+search.value);
})
</script>

<template>
    <div class="home">
        <!--input textarea select 这三个标签支持双向数据绑定-->
        <input type="text" v-model="search" />
        <p v-for="(item,index) in searchNames" :key="index">{{item.name}}</p>
    </div>
</template>

<style scoped>

</style>

watchEffect(()=>{

console.log("watchEffect触发了"+search.value);

})

括号中不需要指定监听的属性

4、停止监听

<script setup>
import { ref, reactive, computed,watch,watchEffect } from "vue";
const names = ref([{ name: "小猫" }, { name: "小狗" }, { name: "小猪" }]);
const search = ref("");
const searchNames = computed(() => {
    return names.value.filter((item) => item.name.includes(search.value));
})
/* watch([search,names],([newSearch,newNames],[prevSearch,prevNames])=>{
    console.log("watch函数触发了",newSearch,prevSearch,newNames,prevNames);
})
watchEffect(()=>{
    console.log("watchEffect触发了"+search.value);
}) */
const stopWatch = watch([search,names],([newSearch,newNames],[prevSearch,prevNames])=>{
    console.log("watch函数触发了",newSearch,prevSearch,newNames,prevNames);
})
const stopEffect = watchEffect(()=>{
    console.log("watchEffect触发了"+search.value);
})
const handleClick=()=>{
    stopWatch();
    stopEffect();
}
</script>

<template>
    <div class="home">
        <!--input textarea select 这三个标签支持双向数据绑定-->
        <input type="text" v-model="search" />
        <button @click="handleClick">停止监听</button>
        <p v-for="(item,index) in searchNames" :key="index">{{item.name}}</p>
    </div>
</template>

<style scoped>

</style>

将监听的函数定义一个属性去接收,然后再定义一个函数里面去调用这个函数就可以停止监听。

const stopEffect = watchEffect(()=>{

console.log("watchEffect触发了"+search.value);

})

const handleClick=()=>{

stopWatch();

stopEffect();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue3watchwatchEffect是用于侦听数据变化并执行副作用的函数,但它们有一些区别。 首先,执行时机不同。watchEffect是立即执行的,在页面加载时会主动执行一次,来收集依赖。而watch是惰性地执行副作用,它不会立即执行,但可以通过配置项immediate来使其在侦听开始时主动触发。 其次,参数不同。watchEffect只需要传递一个回调函数,不需要传递侦听的数据,它会在页面加载时主动执行一次,来收集依赖。而watch至少要有两个参数(第三个参数是配置项),第一个参数是侦听的数据,第二个参数是回调函数。 另外,结果不同。watchEffect只能获取到更改后的值,无法获取到更改前的值。而watch可以同时获取到更改前和更改后的值。 此外,watch API在Vue2也存在,它与Vue3watch API完全等效。watch需要侦听特定的数据源,并在单独的回调函数执行副作用。默认情况下,它也是惰性的,即回调仅在侦听源发生变化时被调用。 最后,watch函数还提供了取消监听的功能。通过调用watch函数会返回一个停止监听的函数,可以在需要停止监听时调用该函数来取消侦听。 综上所述,watchwatchEffectVue3的区别主要体现在执行时机、参数和结果上。 #### 引用[.reference_title] - *1* *2* [【vue3】watchEffectwatch 的区别详解](https://blog.csdn.net/qq_38974163/article/details/122689303)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v4^insert_chatgpt"}} ] [.reference_item] - *3* [watchwatchEffect的区别(vue3)](https://blog.csdn.net/ZXH0122/article/details/129794342)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v4^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱人间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值