vue3父子通信+ref,toRef,toRefs使用实例

ref是什么?

  1. 生成值类型的响应式数据
  2. 可用于模板和reactive
  3. 通过.value修改值
  4. 可以获取DOM元素

<p ref="elemRef">{{nameRef}} -- {{state.name}}</p>

// 获取dom元素

onMounted(()=>{ console.log(elemRef.value); });

toRef是什么?

  1. 针对一个响应式对象(reactive封装)的prop属性!!!
  2. 创建一个ref, 具有响应式
  3. 两者保持引用关系

toRefs是什么?

  1. 将响应式对象(reactive封装)转换为普通对象
  2. 对象的每个prop属性都是对应的ref
  3. 两者保持引用关系

最佳使用方式

  1. 用reactive做对象的响应式, 用ref做值类型响应式
  2. 需要解构响应式对象使用toRefs(state), 只需要获取单个响应式值类型使用toRef(state, 'xxx');
  3. ref的变量命名都用xxRef
  4. 合成函数返回响应式对象时, 用toRefs(usexx这种钩子函数);

使用示例:

  1. 子组件, script标签是这种写法: <script setup lang="ts">时

    <script setup lang="ts"> import { ref, reactive, toRef, toRefs, defineProps } from 'vue'; // 父组件传数据 :msg="xxx" defineProps({ msg: String }); // 子组件通知父组件使用@onSayHello="xxx", 子组件需要使用时eg: emites('onSayHello', 'hello啊啊啊啊') interface IEmits { (e: 'onSayHello', arg1: String): void; } const emites = defineEmits<IEmits>(); const state = reactive({ name: 'alice', age: 20, sex: '女' });

    // 将reactive封装的对象, 使用toRefs获取的对象, 它可以进一步解构, 获取响应式值类型变量
    const stateRef = toRefs(state);
    const { name: nameRef, age: ageRef } = stateRef
    // 将reactive封装的对象, 使用toRef获取某个属性值, 具备响应式
    const sexRef = toRef(state, 'sex')
    const sayHello2 = () => {
    msgRef.value = '你好!'
    emites('onSayHello', 'hello-----')
    }
    // xx.key = ???适用于reactive封装的响应式对象
    const updateState = () => {
    state.name = '双双';
    state.age = 22;
    state.sex = '男';
    // 或者找到响应式值类型,使用 .value进行修改
    // nameRef.value = '双双'
    // ageRef.value = 22
    // sexRef.value = '男'
    }
    // ref值类型, 使用.value进行修改
    const updateRef = () => {
    msgRef.value = 'hello!'
    }

    const msgRef = ref('值类型');
    </script>

    <template>

    {{ msg }}

    {{ msgRef }}, 我叫:{{ nameRef }}, 年龄:{{ ageRef }}, 性别:{{ sexRef }}

    <button @click="sayHello2">打招呼</button> <button @click="updateState">修改名字,年龄,性别</button> <button @click="updateRef">用英文打招呼</button> </template> <style scoped> .read-the-docs { color: #888; }

    button {
    margin: 10px;
    }
    </style>

  2. 子组件, script标签是这种写法: <script>时

    <script> import { ref, reactive, toRef, toRefs } from 'vue' export default { props: { msg: String }, emits: ['onSayHello'], setup(props, { emit }) { console.log(props); // 父组件传进来的数据 const state = reactive({ name: 'alice', age: 20, sex: 1 });
    复制代码
     // 将reactive封装的对象, 使用toRefs获取的对象, 它可以进一步解构, 获取响应式值类型变量
     const stateRef = toRefs(state);
     const { name: nameRef, age: ageRef } = stateRef
     // 将reactive封装的对象, 使用toRef获取某个属性值, 具备响应式
     const sexRef = toRef(state, 'sex')
     const sayHello2 = () => {
       msgRef.value = 'hello, 你好!'
       emit('onSayHello', 'hello-----')
     }
     // xx.key = ???适用于reactive封装的响应式对象
     const updateState = () => {
       state.name = '双双';
       state.age = 22;
       state.sex = 0;
     }
     // ref值类型, 使用.value进行修改
     const updateRef = () => {
       msgRef.value = '你好啊!'
       ageRef.value = 33
       sexRef.value = '男'
     }
    
     const msgRef = ref('值类型');
    
     // 注意要返回变量和方法等模板需要使用的东西, 否则页面不会渲染
     return {
       msgRef,
       sayHello2,
       nameRef,
       ageRef,
       sexRef,
       updateState,
       updateRef,
     }

    }
    }
    </script>

    <template>

    {{ msgRef }}, 我叫:{{ nameRef }}, 年龄:{{ ageRef }}, 性别:{{ sexRef }}

    <button @click="sayHello2">say hello</button> <button @click="updateState">修改state的值</button> <button @click="updateRef">修改ref的值</button> </template> <style scoped> button { margin: 10px; } </style>

父组件: App.vue

复制代码
<script setup>
import HelloWorld from './components/Test2.vue'
function onSayHello(a) {
  console.log(a)
}
</script>

<template>
  <HelloWorld msg="Vite + Vue" @onSayHello="onSayHello"/>
</template>

<style scoped>
.logo.vue:hover {
  filter: drop-shadow(0 0 2em #42b883aa);
}
</style>
相关推荐
前端不太难3 小时前
从 Navigation State 反推架构腐化
前端·架构·react
前端程序猿之路3 小时前
Next.js 入门指南 - 从 Vue 角度的理解
前端·vue.js·语言模型·ai编程·入门·next.js·deepseek
大布布将军3 小时前
⚡️ 深入数据之海:SQL 基础与 ORM 的应用
前端·数据库·经验分享·sql·程序人生·面试·改行学it
川贝枇杷膏cbppg3 小时前
Redis 的 RDB 持久化
前端·redis·bootstrap
D_C_tyu4 小时前
Vue3 + Element Plus | el-table 表格获取排序后的数据
javascript·vue.js·elementui
JIngJaneIL4 小时前
基于java+ vue农产投入线上管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
天外天-亮4 小时前
v-if、v-show、display: none、visibility: hidden区别
前端·javascript·html
jump_jump4 小时前
手写一个 Askama 模板压缩工具
前端·性能优化·rust
hellotutu4 小时前
vue2 从 sessionStorage 手动取 token 后,手动加入到 header
vue.js·token·session·header
be or not to be4 小时前
HTML入门系列:从图片到表单,再到音视频的完整实践
前端·html·音视频