Vue3之watch

  watch监视ref函数

复制代码
<template>
    <h2>当前求和为:{{sum}}</h2>
    <button @click="sum++">点我+1</button>
    <hr>
    <h2>当前的信息为:{{msg}}</h2>
    <button @click="msg+='!'">修改信息</button>
    <hr>
    <h2>姓名:{{person.name}}</h2>
    <h2>年龄:{{person.age}}</h2>
    <h2>薪资:{{person.job.j1.salary}}K</h2>
    <button @click="person.name+='~'">修改姓名</button>
    <button @click="person.age++">增长年龄</button>
    <button @click="person.job.j1.salary++">涨薪</button>
</template>

<script>
    import {ref,reactive,watch} from 'vue'
    export default {
        name: 'Demo',
        setup(){
            //数据
            let sum = ref(0)
            let msg = ref('你好啊')
            let person = ref({
                name:'张三',
                age:18,
                job:{
                    j1:{
                        salary:20
                    }
                }
            })

            console.log(person) 

           // 监视多个ref定义的响应式数据
            watch([sum,msg],(newValue,oldValue)=>{
                console.log('sum或msg变化了',newValue,oldValue)
            }) 
            watch(sum,(newValue,oldValue)=>{
                console.log('sum的值变化了',newValue,oldValue)
            })
//返回一个对象(常用)
            return {
                sum,
                msg,
                person
            }
        }
    }
</script>
复制代码

 watch监视reactive函数

监视reactive定义的响应式数据
  若watch监视的是reactive定义的响应式数据,则无法正确获得oldValue!!
  若watch监视的是reactive定义的响应式数据,则强制开启了深度监视 

 示例:

复制代码
<template>
    <h2>当前求和为:{{sum}}</h2>
    <button @click="sum++">点我+1</button>
    <hr>
    <h2>当前的信息为:{{msg}}</h2>
    <button @click="msg+='!'">修改信息</button>
    <hr>
    <h2>姓名:{{person.name}}</h2>
    <h2>年龄:{{person.age}}</h2>
    <h2>薪资:{{person.job.j1.salary}}K</h2>
    <button @click="person.name+='~'">修改姓名</button>
    <button @click="person.age++">增长年龄</button>
    <button @click="person.job.j1.salary++">涨薪</button>
</template>

<script>
    import {ref,reactive,watch} from 'vue'
    export default {
        name: 'Demo',
        setup(){
            //数据
            let sum = ref(0)
            let msg = ref('你好啊')
            let person = ref({
                name:'张三',
                age:18,
                job:{
                    j1:{
                        salary:20
                    }
                }
            })

            console.log(person) 

           // 监视多个ref定义的响应式数据
            watch([sum,msg],(newValue,oldValue)=>{
                console.log('sum或msg变化了',newValue,oldValue)
            }) 
            watch(sum,(newValue,oldValue)=>{
                console.log('sum的值变化了',newValue,oldValue)
            })

            watch(person,(newValue,oldValue)=>{
                console.log('person的值变化了',newValue,oldValue)
            },{deep:true})
            
            /* 情况三:监视reactive定义的响应式数据
                        若watch监视的是reactive定义的响应式数据,则无法正确获得oldValue!!
                        若watch监视的是reactive定义的响应式数据,则强制开启了深度监视 
            */
            watch(person,(newValue,oldValue)=>{
                console.log('person变化了',newValue,oldValue)
            },{immediate:true,deep:false}) //此处的deep配置不再奏效

            //情况四:监视reactive定义的响应式数据中的某个属性
            watch(()=>person.job,(newValue,oldValue)=>{
                console.log('person的job变化了',newValue,oldValue)
            },{immediate:true,deep:true}) 

            //情况五:监视reactive定义的响应式数据中的某些属性
            watch([()=>person.job,()=>person.name],(newValue,oldValue)=>{
                console.log('person的job变化了',newValue,oldValue)
            },{immediate:true,deep:true})

            //特殊情况
            watch(()=>person.job,(newValue,oldValue)=>{
                console.log('person的job变化了',newValue,oldValue)
            },{deep:true}) //此处由于监视的是reactive素定义的对象中的某个属性,所以deep配置有效

            //返回一个对象(常用)
            return {
                sum,
                msg,
                person
            }
        }
    }
</script>
复制代码

 

  

posted @   安静点--  阅读(131)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示