当前位置: 首页 > news >正文

Vue3 组件通信

目录

create-vue创建项目

一. 父子通信

1. 父传子

2. 子传父

 二. 模版引用(通过ref获取实例对象)

1.基本使用

 2.defineExpose

三. 跨层通信 - provide和inject

1. 作用和场景

2. 跨层传递普通数据

3. 跨层传递响应式数据

4. 跨层传递方法


create-vue创建项目

npm init vue@latest

 

一. 父子通信

1. 父传子

  1. 父组件中给子组件绑定属性

  2. 子组件内部通过props选项接收数据

// 父组件
<script setup>
import sonVue from "./son.vue";
</script>
<template><sonVue msg="this is msg" />
</template>
<style scoped></style>
// 子组件
<script setup>
//子组件内部通过props选项接收数据
const props = defineProps({msg: String,
});
</script>
<template><div>{{ msg }}</div>
</template>
<style scoped></style>

2. 子传父

  1. 父组件中给子组件标签通过@绑定事件

  2. 子组件内部通过 emit 方法触发事件

// 父组件
<script setup>
import sonVue from "./son.vue";
// 获取传递子组件传递的值 val
const getMessage = (val) => {console.log(val);
};
</script>
<template><!-- 1.绑定自定义事件 getMessage --><sonVue @getMessage="getMessage" />
</template>
<style scoped></style>
// 子组件
<script setup>
//2. 生成emit方法
const emit = defineEmits(["getMessage"]);const sendMsg = () => {//3.触发自定义事件,并传递参数emit("getMessage", "this is msg");
};
</script>
<template><button @click="sendMsg">测试</button>
</template>
<style scoped></style>

 二. 模版引用(通过ref获取实例对象)

概念:通过 ref标识 获取真实的 dom对象或者组件实例对象

1.基本使用

  1. 调用ref函数生成一个ref对象

  2. 通过ref标识绑定ref对象到标签  

<script setup>
import { ref } from "vue";
//1.调用ref函数得到ref对象
const TestRef = ref(null);//输出得到一个RefImpl对象
console.log(TestRef);
</script><template><!-- 2. 通过ref标识绑定ref对象 --><div ref="TestRef">测试一下</div>
</template>
<style scoped></style>

 2.defineExpose

  • 默认情况下在 <script setup>语法糖下组件内部的属性和方法是不开放给父组件访问的,为了显式暴露某些属性或方法,可以使用 defineExpose
  • 常用于组件上绑定一个ref属性,来获取需要的某些属性或方法
// 子组件
<script setup>
import { ref } from "vue";
//方法
const count = ref(0);
const setCount = () => {count.value++;
};
//值
const a = ref("this is test data");
const b = ref(2);
defineExpose({a,b,setCount,
});
</script><template><button @click="count">count</button>
</template>
<style scoped></style>
//父组件/页面
<script setup>
import TestDefineExpose from "./components/test2/TestDefineExpose.vue"; //引入const onTest = () => {console.log(Exposeref.value.a);console.log(Exposeref.value.b);console.log(Exposeref.value.setCount);
};
</script><template>
<TestDefineExpose ref="Exposeref" /><button @click="onTest"></button>
</template>

三. 跨层通信 - provide和inject

1. 作用和场景

        顶层组件向任意的底层组件传递数据和方法,实现跨层组件通信

2. 跨层传递普通数据

实现步骤

  1. 顶层组件通过 provide 函数提供数据

  2. 底层组件通过 inject 函数提供数据

 

3. 跨层传递响应式数据

在调用provide函数时,第二个参数设置为ref对象

 

4. 跨层传递方法

 顶层组件可以向底层组件传递方法,底层组件调用方法修改顶层组件的数据

 

http://www.lryc.cn/news/417796.html

相关文章:

  • 如何在Ubuntu 14.04上安装、配置和部署Rocket.Chat
  • ISO 26262中的失效率计算:IEC TR 62380-Section 15-Switches and keyboards
  • Linux安全与高级应用(五)深入探讨Linux Shell脚本应用:从基础到高级
  • Java中等题-解码方法(力扣)
  • 【Git】git 从入门到实战系列(二)—— Git 介绍以及安装方法
  • 【QT 5 QT 6 构建工具qmake-cmake-和-软件编译器MSVCxxxvs MinGWxxx说明】
  • SD卡参数错误:深度解析与数之寻软件恢复实战
  • 深入理解和应用RabbitMQ的Work Queues模型
  • 嵌入式面试八股文(三)·野指针产生原因和解决方法、指针函数和函数指针的区别
  • OpenCV 中 CV_8UC1,CV_32FC3,CV_32S等参数的含义
  • v 3 + vite + ts 自适应布局(postcss-pxtorem)
  • (MTK)java文件添加简单接口并配置相应的SELinux avc 权限笔记2
  • Linux安全与高级应用(六)Linux Shell脚本编程的高级应用:条件测试与if语句的妙用
  • 升级MacOS(Mojave)后使用git问题
  • 基于PFC和ECN搭建无损RoCE网络的工作流程分析
  • 射频功率放大器调测简略
  • Linux使用docker搭建Redis 哨兵模式
  • springboot给类进行赋初值的四种方式
  • Day32 | 1049. 最后一块石头的重量 II 494. 目标和 474.一和零
  • linux 查看一个端口是否被占用
  • 【Git】5. 配置 Git
  • C语言:文件处理
  • SpringBoot MybatisPlus selectOne的坑
  • Spring源码-ClassPathXmlApplicationContext的refresh()都做了什么?
  • 网站加密和混淆技术简介
  • Kafka + Kraft 集群搭建教程,附详细配置及自动化安装脚本
  • “Apple Intelligence”的“系统提示词”被曝光了
  • django学习-数据表操作
  • 机器学习-决策树
  • opencascade TopoDS_Shape源码学习【重中之重】