当前位置: 首页 > news >正文

Vue3中watch用法

在 Vue3 中的组合式 API 中,watch 的作用和 Vue2 中的 watch 作用是一样的,他们都是用来监听响应式状态发生变化的,当响应式状态发生变化时,都会触发一个回调函数。
当需要在数据变化时执行异步或开销较大的操作时,computed是无法操作异步数据的,所以需要使用watch进行侦听。
侦听器watch作用是侦听一个或多个数据的变化,数据变化时执行的回调函数,两个额外参数:immediate(立即执行)和deep(深度侦听)
官网传送门

watch 基本使用

<script setup lang="ts">
import { ref, watch } from 'vue'const message = ref(0)
watch(message, (newValue, oldValue) => {console.log('新的值:', newValue)console.log('旧的值:', oldValue)
})
const changeMsg = () => {message.value++
}
</script>
<template><p>{{ message }}</p><button @click="changeMsg">更改 message</button>
</template>

上段代码中我们点击按钮就会更改响应式变量 message 的值。我们又使用 watch 监听器监听了 message 变量,当它发生变化时,就会触发 watch 监听函数中的回调函数,并且回调函数默认接收两个参数:新值和旧值。

watch 监听类型

ref 和计算属性

<script setup lang="ts">
import { ref, watch, computed } from 'vue'const count = ref(0)
const doubleCount = computed(() => count.value * 2)
watch(doubleCount, (newValue, oldValue) => {console.log('新的值:', newValue)console.log('旧的值:', oldValue)
})
const changeCount = () => {count.value++
}
</script>
<template><p>{{ count }}</p><button @click="changeCount">更改 count</button>
</template>

当我们 count 发生变化时,计算属性 doubleCount也会重新计算得出新的结果,我们 watch 监听函数是可以监听到计算属性变化的。

getter 函数

<script setup lang="ts">
import { ref, watch, computed } from 'vue'const count = ref(0)
const doubleCount = computed(() => count.value * 2)
watch(() => count.value + doubleCount.value,(newValue, oldValue) => {console.log('新的值:', newValue)console.log('旧的值:', oldValue)}
)
const changeCount = () => {count.value++
}
</script>
<template><p>{{ count }}</p><button @click="changeCount">更改 count</button>
</template>

上段代码中 watch 监听器中的第一个参数是一个箭头函数,也就是 getter 函数,getter 函数返回的是响应式数据 count 和 doubleCount 相加的值,当这两个中中有一个变化,都会执行 watch 中的回调函数。有点像是直接把计算属性写到监听器里面去了。

监听响应式对象

<script setup lang="ts">
import { reactive, watch } from 'vue'const count = reactive({ num: 0 })
watch(count, (newValue, oldValue) => {console.log('新的值:', newValue)console.log('旧的值:', oldValue)
})
const changeCount = () => {count.num++
}
</script>
<template><p>{{ count }}</p><button @click="changeCount">更改 count</button>
</template>

当 watch 监听的是一个响应式对象时,会隐式地创建一个深层侦听器,即该响应式对象里面的任何属性发生变化,都会触发监听函数中的回调函数。

监听多个来源的数组

<script setup lang="ts">
import { ref, watch, computed } from 'vue'const count = ref(0)
const doubleCount = computed(() => count.value * 2)
watch([count, doubleCount], (newValue, oldValue) => {console.log('新的值:', newValue)console.log('旧的值:', oldValue)
})
const changeCount = () => {count.value++
}
</script>
<template><p>{{ count }}</p><button @click="changeCount">更改 count</button>
</template>

深度监听

在前面的代码中,如果我们将一个响应式对象传递给 watch 监听器时,只要对象里面的某个属性发生了变化,那么就会执行监听器回调函数。

究其原因,因为我们传入响应对象给 watch 时,隐式的添加一个深度监听器,这就让我们造成了我们牵一发而至全身的效果。

但是,如果我们是使用的 ref响应式对象的形式,那么响应式对象的属性值发生变化,是不会触发 watch 的回调函数的。

<script setup lang="ts">
import { ref, watch, computed } from 'vue'const count = ref({ num: 0 })
watch(count,(newValue, oldValue) => {console.log('新的值:', newValue)console.log('旧的值:', oldValue)}
)
const changeCount = () => {count.value.num++
}
</script>
<template><p>{{ count }}</p><button @click="changeCount">更改 count</button>
</template>

上段代码中我们使用 ref响应式对象,当我们更改 number 中 count 的值时,watch 的回调函数是不会执行的。

为了实现上述代码的监听,我们可以手动给监听器加上深度监听的效果。

<script setup lang="ts">
import { ref, watch, computed } from 'vue'const count = ref({ num: 0 })
watch(count,(newValue, oldValue) => {console.log('新的值:', newValue)console.log('旧的值:', oldValue)},{ deep: true }
)
const changeCount = () => {count.value.num++
}
</script>
<template><p>{{ count }}</p><button @click="changeCount">更改 count</button>
</template>

immediate

在侦听器创建时立即触发回调,响应式数据变化之后继续执行回调,用法如下:

<script setup lang="ts">
import { ref, watch } from 'vue'const count = ref({ num: 0 })
watch(count,(newValue, oldValue) => {console.log('新的值:', newValue)console.log('旧的值:', oldValue)},{ immediate: true }
)
const changeCount = () => {count.value.num++
}
</script>
<template><p>{{ count }}</p><button @click="changeCount">更改 count</button>
</template>

watch监听回调函数会再创建时立即执行

提示

前面我们一直强调 watch 监听的是响应式数据,如果我们监听的数据不是响应式的,那么可能会抛出如下警告:

runtime-core.esm-bundler.js:41 [Vue warn]: Invalid watch source:  123 A watch source can only be a getter/effect function, a ref, a reactive object, or an array of these types. at <Index onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< null > > at <RouterView> at <ElMain> at <ElContainer> at <ElContainer class="layout-container" > at <LayoutPage onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< Proxy(Object) > > at <RouterView> at <App>

在这里插入图片描述

http://www.lryc.cn/news/170492.html

相关文章:

  • 组里来了一个实习生,一行代码引发了一个惨案
  • 随手笔记(四十五)——idea git冲突
  • chacha20 算法流程
  • 准备篇(三)Python 爬虫第三方库
  • 从零开始的PICO开发教程(4)-- VR世界 射线传送、旋转和移动
  • 防止攥改之水印功能组件
  • iOS 17 适配 Xcode 15 问题
  • Element Plus 快速开始
  • 华为云云耀云服务器L实例评测|StackEdit中文版在线Markdown笔记工具
  • MyEclipse报错javax/persistence/EntityManagerFactory
  • 【MySQL进阶】SQL性能分析
  • 在SpringBoot项目中整合SpringSession,基于Redis实现对Session的管理和事件监听
  • 浅析vue中computed,method,watch,watchEffect的区别
  • activiti7的数据表和字段的解释
  • Java手写Trie树和Trie树应用拓展案例
  • alova.js快速入门教程
  • 获取IP地址-根据IP获取位置信息
  • Android13适配-Google官方照片视频选择器
  • 云计算的发展趋势和挑战
  • PyG-GAT-Cora(在Cora数据集上应用GAT做节点分类)
  • java专项练习(验证码)
  • MS1861 视频处理与显示控制器 HDMI转MIPI LVDS转MIPI带旋转功能 图像带缩放,旋转,锐化
  • 广州华锐互动:利用VR复原文化遗址,沉浸式体验历史文物古迹的魅力
  • 微信小程序——事件监听
  • View绘制流程的源码所得
  • 企业级数据仓库-理论知识
  • 解决flutter不识别yaml里面配置的git项目
  • rust结构体
  • Python - 小玩意 - 键盘记录器
  • msvcp71.dll丢失的解决方法分享,全面分析msvcp71.dll丢失原因