Vue3新特性之computed计算属性和watch监听属性

setup(){

    let name=ref('张三')

    let a=ref(0)

    let b=ref(1)

    let person=reactive({

        firstName:'李',

        lastName:'四',

        sex:'男',

        job:{

            city:{

                salary:'10000'

            }

        }

    })

    一、computed计算属性新特性

    1.简写,不考虑计算属性被修改的情况     

let fullName=computed(()=>{

            return person.firstName+'-'+person.lastName

        })

    2.详细,给计算属性配getter和setter

    let fullName=computed(

        get(){

            return person.firstName+'-'+person.lastName

        }

        set(value){

            const newValue=value.split('')

            person.firstName=newValue[0]

            person.lastName=newValue[1]

        }

    )

    二、watch监听属性新特性

    watch(value,callback,options)值,值改变所触发的回调函数,配置项(immediate,deep默认为true,设置fasle无效)

    1、watch监听单个ref对象   

 watch(name,(newValue,oldValue)=>{

        console.log('newValue......',newValue)

        console.log('oldValue......',oldValue)

        console.log(name发生改变了)

    })

    2、watch监听多个ref对象   

 watch([name,a,b],(newValue,oldValue)=>{

        console.log('newValue......',newValue)

        console.log('oldValue......',oldValue)

        //输出两者都是数组形式

        console.log(name或a或b发生改变了)

    })

    3、watch监视reactive数据中的所有数据

    当改变person中的属性时   

  watch(person,(newValue,oldValue)=>{

        console.log('newValue......',newValue)

        console.log('oldValue......',oldValue)

        console.log(person发生改变了)

    })

    此时会报出person发生了变化,但是新值和旧值一样

    4、watch监听reactive数据中的某个属性

     watch(person.firstName,(newValue,oldValue)=>{

        console.log('newValue......',newValue)

        console.log('oldValue......',oldValue)

        console.log(person发生改变了)

    })

切记不能这么写,person.name是个死数据,watch监听的是ref,reactive,数组,函数等类型数据

    所以我们需要将其写成函数形式,此时新值和旧值不一样,正常输出   

 watch(()=>person.firstName,(newValue,oldValue)=>{

        console.log('newValue......',newValue)

        console.log('oldValue......',oldValue)

        console.log(person发生改变了)

    })

    5、watch监听reactive数据中的多个属性     

watch([()=>person.firstName,()=>{person.lastName}],(newValue,oldValue)=>{

        console.log('newValue......',newValue)

        console.log('oldValue......',oldValue)

        console.log(person发生改变了)

    })

    6、特殊情况   

 watch(()=>person.job,(newValue,oldValue)=>{

        console.log('newValue......',newValue)

        console.log('oldValue......',oldValue)

        console.log(person发生改变了)

    })

    此时当改变job里面的salary时watch检测不到,需要在配置项中开启深度监视,但此时新值和旧值依旧一样

    7、总结

    watch监听基本类型的数据时,新值和旧值正常有效

    但是监听对象类型时新值和旧值一样(无效)

    当watch监听reactive对象时默认开启深度监视,但监视reactive对象的的某个属性(此属性为复杂数据类型)改变时需要手动开启深度监视

    }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端摸鱼侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值