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();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱人间

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

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

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

打赏作者

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

抵扣说明:

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

余额充值