首页 > 其他分享 >Vue3之toRef

Vue3之toRef

时间:2022-12-27 21:55:23浏览次数:41  
标签:const name age toRefs toRef person Vue3

toRef

  • 作用:创建一个 ref 对象,其value值指向另一个对象中的某个属性。

  • 语法:const name = toRef(person,'name')

  • 应用: 要将响应式对象中的某个属性单独提供给外部使用时。

  • 扩展:toRefs 与toRef功能一致,但可以批量创建多个 ref 对象,语法:toRefs(person)

 

<template>
    <h4>{{person}}</h4>
    <h2>姓名:{{name}}</h2>
    <h2>年龄:{{age}}</h2>
    <h2>薪资:{{job.j1.salary}}K</h2>
    <button @click="name+='~'">修改姓名</button>
    <button @click="age++">增长年龄</button>
    <button @click="job.j1.salary++">涨薪</button>
</template>

<script>
    import {ref,reactive,toRef,toRefs} from 'vue'
    export default {
        name: 'Demo',
        setup(){
            //数据
            let person = reactive({
                name:'张三',
                age:18,
                job:{
                    j1:{
                        salary:20
                    }
                }
            })

            // const name1 = person.name
            // console.log('%%%',name1)

            // const name2 = toRef(person,'name')
            // console.log('####',name2)

            // 直接将
            const x = toRefs(person)
            console.log('******',x)

          //返回一个对象 这种写法不支持响应式,你就算更改了name,age,也不会更改到Person中的name,age属性值
            // return {
            //     person,
            //       name:person.name,
            //       age: person.age , 
            // }

            //返回一个对象(常用)支持响应式
            return {
                person,
                // name:toRef(person,'name'),
                // age:toRef(person,'age'),
                // salary:toRef(person.job.j1,'salary'),
                // 也可以直接这样写,使用...扩展运算符,将person中的每一项拆出来
                ...toRefs(person)
            }
        }
    }
</script>

 

  

标签:const,name,age,toRefs,toRef,person,Vue3
From: https://www.cnblogs.com/anjingdian/p/17009093.html

相关文章

  • Vue3生命周期
    Vue3.0中可以继续使用Vue2.x中的生命周期钩子,但有有两个被更名:beforeDestroy改名为 beforeUnmountdestroyed改名为 unmountedVue3.0也提供了CompositionAPI形式......
  • Vue3之watchEffect函数
    watchEffect函数watch的套路是:既要指明监视的属性,也要指明监视的回调。watchEffect的套路是:不用指明监视哪个属性,监视的回调中用到哪个属性,那就监视哪个属性。wat......
  • vue3 项目中设置组件name
    在开发vue3项目时,如果使用的是setup语法,那么想要给组件设置name属性。可以通过下面的两种形式。在组件中额外创建一个script脚本,在其中采用选项式api的写法,设置name属性......
  • Vue3 企业级优雅实战 - 组件库框架 - 9 实现组件库 cli - 上
    上文搭建了组件库cli的基础架子,实现了创建组件时的用户交互,但遗留了cli/src/command/create-component.ts中的createNewComponent函数,该函数要实现的功能就是上文开......
  • vue3传值
    https://blog.csdn.net/H_114/article/details/122420402?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERat......
  • 【Vue2.0与Vue3.0区别总结】
    vue2.0和vue3.0区别结构部分程序主入口文件main.jsvue2.0vue3.0-暴露出createApp方法组件语法部分(vue3.0主要针对api的优化,vue3.0兼容vue2.0写法)vue3.0使用​​组......
  • Vue3之watch
     watch监视ref函数<template><h2>当前求和为:{{sum}}</h2><button@click="sum++">点我+1</button><hr><h2>当前的信息为:{{msg}}</h2><button......
  • Vue3之computed计算属性
    计算属性  computed函数与Vue2.x中computed配置功能一致写法<template><h1>一个人的信息</h1>姓:<inputtype="text"v-model="person.firstName">......
  • vue3_05使用reactive来处理复杂数据
    vue3中除了提供了ref函数以为还提供了reactive函数来操作数据,一般情况下我们使用ref函数来操作简单类型数据,reactive函数来操作复杂类型数据<template><div>{{objRet.na......
  • vue3_03ref操作复杂类型
    ref也可以将复杂类型的数据转换为响应式数据,使用方法和处理简单类型数据一样leta=ref(复杂类型数据)a.value.xxx<template><p>{{objref.num}}</p><button@c......