• VUE3使用watch监测数据变化


     1 // 不要忘了导入要用的 API
     2 import { defineComponent, reactive, watch } from 'vue'
     3 
     4 export default defineComponent({
     5   setup() {
     6     // 定义一个响应式数据
     7     const userInfo = reactive({
     8       name: 'Petter',
     9       age: 18,
    10     })
    11 
    12     // 2s后改变数据
    13     setTimeout(() => {
    14       userInfo.name = 'Tom'
    15     }, 2000)
    16 
    17     /**
    18      * 可以直接监听这个响应式对象
    19      * callback 的参数如果不用可以不写
    20      */
    21     watch(userInfo, () => {
    22       console.log('监听整个 userInfo ', userInfo.name)
    23     })
    24 
    25     /**
    26      * 也可以监听对象里面的某个值
    27      * 此时数据源需要写成 getter 函数
    28      */
    29     watch(
    30       // 数据源,getter 形式
    31       () => userInfo.name,
    32       // 回调函数 callback
    33       (newValue, oldValue) => {
    34         console.log('只监听 name 的变化 ', userInfo.name)
    35         console.log('打印变化前后的值', { oldValue, newValue })
    36       }
    37     )
    38   },
    39 })

    批量监测

     1 import { defineComponent, ref, watch } from 'vue'
     2 
     3 export default defineComponent({
     4   setup() {
     5     // 定义多个数据源
     6     const message = ref<string>('')
     7     const index = ref<number>(0)
     8 
     9     // 2s后改变数据
    10     setTimeout(() => {
    11       message.value = 'Hello World!'
    12       index.value++
    13     }, 2000)
    14 
    15     watch(
    16       // 数据源改成了数组
    17       [message, index],
    18       // 回调的入参也变成了数组,每个数组里面的顺序和数据源数组排序一致
    19       ([newMessage, newIndex], [oldMessage, oldIndex]) => {
    20         console.log('message 的变化', { newMessage, oldMessage })
    21         console.log('index 的变化', { newIndex, oldIndex })
    22       }
    23     )
    24   },
    25 })

    深度监听

     1 import { defineComponent, ref, watch } from 'vue'
     2 
     3 export default defineComponent({
     4   setup() {
     5     // 定义一个响应式数据,注意我是用的 ref 来定义
     6     const nums = ref<number[]>([])
     7 
     8     // 2s后给这个数组添加项目
     9     setTimeout(() => {
    10       nums.value.push(1)
    11 
    12       // 可以打印一下,确保数据确实变化了
    13       console.log('修改后', nums.value)
    14     }, 2000)
    15 
    16     // 但是这个 watch 不会按预期执行
    17     watch(
    18       nums,
    19       // 这里的 callback 不会被触发
    20       () => {
    21         console.log('触发监听', nums.value)
    22       },
    23       // 因为关闭了 deep
    24       {
    25         deep: false,
    26       }
    27     )
    28   },
    29 })

    立即执行

     1 import { defineComponent, ref, watch } from 'vue'
     2 
     3 export default defineComponent({
     4   setup() {
     5     // 这个时候不会触发 watch 的回调
     6     const message = ref<string>('')
     7 
     8     // 2s后改变数据
     9     setTimeout(() => {
    10       // 来到这里才会触发 watch 的回调
    11       message.value = 'Hello World!'
    12     }, 2000)
    13 
    14     watch(message, () => {
    15       console.log('触发监听', message.value)
    16     })
    17   },
    18 })
     1 import { defineComponent, ref, watch } from 'vue'
     2 
     3 export default defineComponent({
     4   setup() {
     5     // 这一次在这里可以会触发 watch 的回调了
     6     const message = ref<string>('')
     7 
     8     // 2s后改变数据
     9     setTimeout(() => {
    10       // 这一次,这里是第二次触发 watch 的回调,不再是第一次
    11       message.value = 'Hello World!'
    12     }, 2000)
    13 
    14     watch(
    15       message,
    16       () => {
    17         console.log('触发监听', message.value)
    18       },
    19       // 设置 immediate 选项
    20       {
    21         immediate: true,
    22       }
    23     )
    24   },
    25 })

    watchEffect 

     1 export default defineComponent({
     2   setup() {
     3     const foo = ref<string>('')
     4 
     5     setTimeout(() => {
     6       foo.value = 'Hello World!'
     7     }, 2000)
     8 
     9     function bar() {
    10       console.log(foo.value)
    11     }
    12 
    13     // 使用 watch 需要先手动执行一次
    14     bar()
    15 
    16     // 然后当 foo 有变动时,才会通过 watch 来执行 bar()
    17     watch(foo, bar)
    18   },
    19 })
     1 export default defineComponent({
     2   setup() {
     3     const foo = ref<string>('')
     4 
     5     setTimeout(() => {
     6       foo.value = 'Hello World!'
     7     }, 2000)
     8 
     9     function bar() {
    10       console.log(foo.value)
    11     }
    12 
    13     // 可以通过 watchEffect 实现 bar() + watch(foo, bar) 的效果
    14     watchEffect(bar)
    15   },
    16 })

    来自于: https://vue3.chengpeiquan.com/component.html#基础用法

  • 相关阅读:
    JZYZOJ1311 邮局设置问题 dp
    备忘录和乱七八糟的体会
    Python strip、lstrip和rstrip的用法
    Linux jstack命令详解
    Linux EOF使用
    Linux 千万不要执行的10个命令
    Linux 浅谈Linux 操作系统的安全设置
    linux 使用 ionice 限制 Xen 虚拟机磁盘 IO
    Linux 实现rsyslog日志里面的IP地址记录 未测试
    linux 回收站的添加
  • 原文地址:https://www.cnblogs.com/googlegis/p/16460179.html
Copyright © 2020-2023  润新知