• 【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>

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

  • 相关阅读:
    用案例告诉你,数据库的备份还原如此简单!!!
    Java代码中如何向HashMap对象中添加(Map集合对象)呢?
    大数据毕业设计选题推荐-收视点播数据分析-Hadoop-Spark-Hive
    plink如何更新表型数据
    MATLAB算法实战应用案例精讲-【图像处理】机器视觉(补充篇)
    数据安全合格有哪些要求
    Netty入门学习
    词云的可视化设计教程
    模块电路选型(1)----电源模块
    数据库变更管理:Liquibase or Flyway
  • 原文地址:https://blog.csdn.net/dxnn520/article/details/126463831