Vue3的watch侦听器和watchEffect高级侦听器如何实现复杂数据变化响应?
- 内容介绍
- 文章标签
- 相关推荐
本文共计846个文字,预计阅读时间需要4分钟。
目录
1.watch 监听器
2.watchEffect 高级监听器 + 清除副作用
在 Vue 开发中,watch 和 watchEffect 是用于监听数据变化并进行相应操作的重要工具。以下是它们的简要介绍:
1. watch 监听器
- 用于监听数据的变化,当数据变化时,会自动执行回调函数。 - 适用于简单的情况,如直接监听一个变量。2. watchEffect 高级监听器 + 清除副作用 - 在数据变化时,自动执行传入的回调函数,并自动收集依赖。 - 清除副作用:在组件销毁前,自动停止副作用,防止内存泄漏。 - 应用场景:在触发展望时调用函数处理逻辑,如防抖、停止追踪、调用函数后停止更新等。
目录
- 1watch侦听器
- 2watchEffect高级侦听器
- 清除副作用:就是在触发监听之前会调用一个函数可以处理你的逻辑例如防抖
- 停止跟踪 watchEffect 返回一个函数 调用之后将停止更新
1watch侦听器
watch 需要侦听特定的数据源,并在单独的回调函数中执行副作用
- watch第一个参数监听源
- watch第二个参数回调函数cb(newVal,oldVal)
- watch第三个参数一个options配置项是一个对
- {
- immediate:true //是否立即调用一次
- deep:true //是否开启深度监听
- }
监听Ref 案例:
import { ref, watch } from 'vue' let message = ref({ nav:{ bar:{ name:"" } } }) watch(message, (newVal, oldVal) => { console.log('新的值----', newVal); console.log('旧的值----', oldVal); },{ immediate:true, deep:true
监听多个ref 注意变成数组:
import { ref, watch ,reactive} from 'vue' let message = ref('') let message2 = ref('') watch([message,message2], (newVal, oldVal) => { console.log('新的值----', newVal); console.log('旧的值----', oldVal); })
监听Reactive:使用reactive监听深层对象开启和不开启deep 效果一样
import { ref, watch ,reactive} from 'vue' let message = reactive({ nav:{ bar:{ name:"" } } }) watch(message, (newVal, oldVal) => { console.log('新的值----', newVal); console.log('旧的值----', oldVal); })
监听reactive 单一值
import { ref, watch ,reactive} from 'vue' let message = reactive({ name:"", name2:"" }) watch(()=>message.name, (newVal, oldVal) => { console.log('新的值----', newVal); console.log('旧的值----', oldVal); })
2watchEffect高级侦听器
立即执行传入的一个函数,同时响应式追踪其依赖,并在其依赖变更时重新运行该函数。如果用到message 就只会监听message 就是用到几个监听几个 而且是非惰性 会默认调用一次。
let message = ref<string>('') let message2 = ref<string>('') watchEffect(() => { //console.log('message', message.value); console.log('message2', message2.value); })
清除副作用:就是在触发监听之前会调用一个函数可以处理你的逻辑例如防抖
import { watchEffect, ref } from 'vue' let message = ref<string>('') let message2 = ref<string>('') watchEffect((oninvalidate) => { //console.log('message', message.value); oninvalidate(()=>{ }) console.log('message2', message2.value); })
停止跟踪 watchEffect 返回一个函数 调用之后将停止更新
const stop = watchEffect((oninvalidate) => { //console.log('message', message.value); oninvalidate(()=>{ }) console.log('message2', message2.value); },{ flush:"post",// pre:组件更新前执行;sync:强制效果始终同步触发,post:组件更新后执行 onTrigger () { //onTrigger 可以帮助我们调试 watchEffect } }) stop()
到此这篇关于Vue3中的watch侦听器和watchEffect高级侦听器的文章就介绍到这了,更多相关Vue3watch侦听器和watchEffect侦听器内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!
本文共计846个文字,预计阅读时间需要4分钟。
目录
1.watch 监听器
2.watchEffect 高级监听器 + 清除副作用
在 Vue 开发中,watch 和 watchEffect 是用于监听数据变化并进行相应操作的重要工具。以下是它们的简要介绍:
1. watch 监听器
- 用于监听数据的变化,当数据变化时,会自动执行回调函数。 - 适用于简单的情况,如直接监听一个变量。2. watchEffect 高级监听器 + 清除副作用 - 在数据变化时,自动执行传入的回调函数,并自动收集依赖。 - 清除副作用:在组件销毁前,自动停止副作用,防止内存泄漏。 - 应用场景:在触发展望时调用函数处理逻辑,如防抖、停止追踪、调用函数后停止更新等。
目录
- 1watch侦听器
- 2watchEffect高级侦听器
- 清除副作用:就是在触发监听之前会调用一个函数可以处理你的逻辑例如防抖
- 停止跟踪 watchEffect 返回一个函数 调用之后将停止更新
1watch侦听器
watch 需要侦听特定的数据源,并在单独的回调函数中执行副作用
- watch第一个参数监听源
- watch第二个参数回调函数cb(newVal,oldVal)
- watch第三个参数一个options配置项是一个对
- {
- immediate:true //是否立即调用一次
- deep:true //是否开启深度监听
- }
监听Ref 案例:
import { ref, watch } from 'vue' let message = ref({ nav:{ bar:{ name:"" } } }) watch(message, (newVal, oldVal) => { console.log('新的值----', newVal); console.log('旧的值----', oldVal); },{ immediate:true, deep:true
监听多个ref 注意变成数组:
import { ref, watch ,reactive} from 'vue' let message = ref('') let message2 = ref('') watch([message,message2], (newVal, oldVal) => { console.log('新的值----', newVal); console.log('旧的值----', oldVal); })
监听Reactive:使用reactive监听深层对象开启和不开启deep 效果一样
import { ref, watch ,reactive} from 'vue' let message = reactive({ nav:{ bar:{ name:"" } } }) watch(message, (newVal, oldVal) => { console.log('新的值----', newVal); console.log('旧的值----', oldVal); })
监听reactive 单一值
import { ref, watch ,reactive} from 'vue' let message = reactive({ name:"", name2:"" }) watch(()=>message.name, (newVal, oldVal) => { console.log('新的值----', newVal); console.log('旧的值----', oldVal); })
2watchEffect高级侦听器
立即执行传入的一个函数,同时响应式追踪其依赖,并在其依赖变更时重新运行该函数。如果用到message 就只会监听message 就是用到几个监听几个 而且是非惰性 会默认调用一次。
let message = ref<string>('') let message2 = ref<string>('') watchEffect(() => { //console.log('message', message.value); console.log('message2', message2.value); })
清除副作用:就是在触发监听之前会调用一个函数可以处理你的逻辑例如防抖
import { watchEffect, ref } from 'vue' let message = ref<string>('') let message2 = ref<string>('') watchEffect((oninvalidate) => { //console.log('message', message.value); oninvalidate(()=>{ }) console.log('message2', message2.value); })
停止跟踪 watchEffect 返回一个函数 调用之后将停止更新
const stop = watchEffect((oninvalidate) => { //console.log('message', message.value); oninvalidate(()=>{ }) console.log('message2', message2.value); },{ flush:"post",// pre:组件更新前执行;sync:强制效果始终同步触发,post:组件更新后执行 onTrigger () { //onTrigger 可以帮助我们调试 watchEffect } }) stop()
到此这篇关于Vue3中的watch侦听器和watchEffect高级侦听器的文章就介绍到这了,更多相关Vue3watch侦听器和watchEffect侦听器内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

