• 【Vue3】Vue3中监视watch和watchEffect使用(图文+代码)


    前言:watch结构三部分:

    1、监视变量名,

    2、监视回调值(新值newValue和旧值oldValue)

     3、监视参数

    参数解读:

    1、immediate:true:监听的这个对象是否会【立始输出】,也就是监听没开启动作时,就先输入初始值。

    2、deep:true  是否深度监视,也就是针对对象或数组内的值监视。

    一、监视ref所定义的一个响应式数据

     

    1. <template>
    2. <h2>当前求和为:{{ sum }}</h2>
    3. <button @click="sum++">点我sum++</button>
    4. </template>
    5. <script>
    6. import {ref, watch} from 'vue'
    7. export default {
    8. name: 'Demo',
    9. setup() {
    10. let sum = ref(0);
    11. let msg = ref("你好啊");
    12. //情况一:监视ref所定义的一个响应式数据
    13. watch(sum, (newValue, oldValue) => {
    14. console.log("sum发生了变化", newValue, oldValue);
    15. })
    16. return {
    17. sum
    18. }
    19. }
    20. }
    21. </script>

    1、immediate:true:监听的这个对象是否会【立始输出】,也就是监听没开启动作时,就先输入初始值。

    2、deep:true  是否深度监视,也就是针对对象或数组内的值监视。

    二、多个变量同时监视时

     

    1. <template>
    2. <h2>当前求和为:{{ sum }}</h2>
    3. <button @click="sum++">点我sum++</button>
    4. <hr/>
    5. <h2>信息为:{{ msg }}</h2>
    6. <button @click="msg+='!'">点我sum++</button>
    7. </template>
    8. <script>
    9. import {ref, watch} from 'vue'
    10. export default {
    11. name: 'Demo',
    12. setup() {
    13. let sum = ref(0);
    14. let msg = ref("你好啊");
    15. //情况二:监视ref所定义的多个响应式数据
    16. watch([sum,msg],(newValue, oldValue) => {
    17. console.log("sum发生了变化", newValue, oldValue);
    18. })
    19. return {
    20. sum,
    21. msg
    22. }
    23. }
    24. }
    25. </script>

    三、监视reactive所定义的一个响应式数据的整个数组对象变化

     

    1. <template>
    2. <h2>姓名:{{ person.name }}</h2>
    3. <h2>年龄:{{ person.age }}</h2>
    4. <h2>薪资:{{ person.job.j1.salary }}K</h2>
    5. <button @click="person.name+='~'">修改姓名</button>
    6. <button @click="person.age++">修改年龄</button>
    7. <button @click="person.job.j1.salary++">涨薪</button>
    8. </template>
    9. <script>
    10. import {reactive, watch} from 'vue'
    11. export default {
    12. name: 'Demo',
    13. setup() {
    14. let person = reactive({
    15. name: "张三",
    16. age: 18,
    17. job:{
    18. j1:{
    19. salary:20
    20. }
    21. }
    22. })
    23. //情况三:监视reactive所定义的一个响应式数据全部属性
    24. // 1\注意:无法正确获取oldvalue
    25. // 2\注意:强制开启了深度监视(deep配置无效)
    26. watch(person, (newValue, oldValue) => {
    27. console.log("person发生了变化", newValue, oldValue);
    28. })
    29. return {
    30. person
    31. }
    32. }
    33. }
    34. </script>

    注意:无法正确获取oldvalue

    四、监视reactive所定义的一个响应式数据某个属性(对象数组的某个值

     

    1. <template>
    2. <h2>姓名:{{ person.name }}</h2>
    3. <h2>年龄:{{ person.age }}</h2>
    4. <h2>薪资:{{ person.job.j1.salary }}K</h2>
    5. <button @click="person.name+='~'">修改姓名</button>
    6. <button @click="person.age++">修改年龄</button>
    7. <button @click="person.job.j1.salary++">涨薪</button>
    8. </template>
    9. <script>
    10. import {reactive, watch} from 'vue'
    11. export default {
    12. name: 'Demo',
    13. setup() {
    14. let person = reactive({
    15. name: "张三",
    16. age: 18,
    17. job:{
    18. j1:{
    19. salary:20
    20. }
    21. }
    22. })
    23. watch(()=>person.name, (newValue, oldValue) => {
    24. console.log("person发生了变化", newValue, oldValue);
    25. })
    26. return {
    27. person
    28. }
    29. }
    30. }
    31. </script>

    五、监视reactive所定义的一个响应式数据某个属性(对象数组中的子对象数组的变化

    增加deep:true参数

     

     

    1. <template>
    2. <h2>姓名:{{ person.name }}</h2>
    3. <h2>年龄:{{ person.age }}</h2>
    4. <h2>薪资:{{ person.job.j1.salary }}K</h2>
    5. <button @click="person.name+='~'">修改姓名</button>
    6. <button @click="person.age++">修改年龄</button>
    7. <button @click="person.job.j1.salary++">涨薪</button>
    8. </template>
    9. <script>
    10. import {reactive, watch} from 'vue'
    11. export default {
    12. name: 'Demo',
    13. setup() {
    14. let person = reactive({
    15. name: "张三",
    16. age: 18,
    17. job:{
    18. j1:{
    19. salary:20
    20. }
    21. }
    22. })
    23. watch(()=>person.job, (newValue, oldValue) => {
    24. console.log("job发生了变化", newValue, oldValue);
    25. },{deep:true})
    26. return {
    27. person
    28. }
    29. }
    30. }
    31. </script>

    六、watchEffect使用

    watch:提前指定具体变量元素,同时需要写返回值。
    watchEffect:不提前指定具体元素,不需要返回值,有点像computed。

    如下例:

    const xl = person.age,变量指向person.age,person.age改变时,就是被监视

     

     

    1. <template>
    2. <h2>姓名:{{ person.name }}</h2>
    3. <h2>年龄:{{ person.age }}</h2>
    4. <h2>薪资:{{ person.job.j1.salary }}K</h2>
    5. <button @click="person.name += '~'">修改姓名</button>
    6. <button @click="person.age++">修改年龄</button>
    7. <button @click="person.job.j1.salary++">涨薪</button>
    8. </template>
    9. <script>
    10. import { reactive, watchEffect } from 'vue'
    11. export default {
    12. name: 'Demo',
    13. setup() {
    14. let person = reactive({
    15. name: "张三",
    16. age: 18,
    17. job: {
    18. j1: {
    19. salary: 20
    20. }
    21. }
    22. })
    23. //watchEffect所指定的回调中用到的数据只要发生变化,则直接重新执行回调
    24. watchEffect(() => {
    25. const xl = person.age
    26. console.log("watchEffect配置的回调执行了")
    27. })
    28. return {
    29. person
    30. }
    31. }
    32. }
    33. </script>

    最后,感谢:“上归谷”的张老师

  • 相关阅读:
    springboot+java大学生西部计划志愿者岗位补助管理系统
    【数仓建设系列之五】数仓选型架构概览
    C# 入坑JAVA 潜规则 大小写敏感文件名和类名 枚举等 入门系列2
    Shell脚本中常见的几种变量类型
    HP E1406A 端子模块
    UML简介
    英伟达显卡【NVIDIA GeForece RTX3060 laptop GPU】装深度学习环境,学习框架为Pytorch
    基于Spring AOP和CGLIB代理实现引介增强(Introduction Advice)示例
    BioVendor人Clara细胞蛋白(CC16)Elisa试剂盒检测步骤
    人工智能在无人驾驶领域有哪些方面的运用和应用
  • 原文地址:https://blog.csdn.net/dxnn520/article/details/126463831