Vue3-Props 组件数据传递
最编程
2024-04-27 13:40:38
...
Props组件传递(父传子)
一、前言
-
在
vue3
的setup
中,我们使用defineProps
来定义父组件传递的props
。 -
在setup语法糖中用
defineProps
(defineProps
是编译器宏,无需引入)。
二、defineProps使用
子组件:Child.vue
<template>
<h1>{{ message }},num={{ num }}</h1>
</template>
<script setup>
const props = defineProps({
message: {
type: String,
require: true,
default: "默认值"
},
num: Number,
});
</script>
父组件:Parent.vue
<template>
<h1>Parent</h1>
<Child message="hello" :num="123"/>
</template>
<script setup>
import Child from "./components/Child.vue";
</script>
1.定义后
props
值可直接在模板中使用或赋值给data对象。2.传数值类型或者复杂类型时需要加‘:’传递。
2.由
defineProps
定义出的props
对象,是一个proxy
对象,所有特性和reactive
基本相同,只不过由defineProps
定义出的props
对象的值是只读的,还有在模板上可以单独属性直接使用。
三、注意事项
-
defineProps只能在setup中使用,且只能在setup的顶层使用,不能在局部作用域使用。
-
和vue2一样的原则,defineProps声明的props的值是只读readonly的,不能修改,也就是说,当传过来的是原始值时不能修改,当是对象类型时,不能修改对象类型的引用地址。
Props组件传递(子传父)
一、前言
在vue3中使用props实现子传父的原理是使用props的function数据类型来实现。
二、案例
Child.vue
<template>
<h1>{{ title }}</h1>
</template>
<script setup>
import { onMounted } from "vue";
const props = defineProps({
title: String,
onEvent: Function, //Function类型
});
onMounted(() => {
props.onEvent("hello");
});
</script>
Parent.vue
<template>
<h1>Parent</h1>
<Child :onEvent="dataFn" />
</template>
<script setup>
import { reactive } from "vue";
import Child from "./components/Child.vue";
const stateData = reactive({})
const dataFn = (data) => {
stateData.message = data
};
</script>
1.父组件传递dataFn函数给子组件调用,子组件使用onEvent函数实现对dataFn的调用并传参,从而父组件里的函数可以接收到传递的参数,实现了子传父。
推荐阅读
-
Vue 入门:风格冲突范围、数据函数、组件通信、道具与数据单向流、打包与发布
-
Flutter 配置路由组件提取和页面传递参数
-
Vue3-Props 组件数据传递
-
一种结构设计模式,允许在对象中动态添加新行为。它通过创建一个封装器来实现这一目的,即把对象放入一个装饰器类中,然后把这个装饰器类放入另一个装饰器类中,以此类推,形成一个封装器链。这样,我们就可以在不改变原始对象的情况下动态添加新行为或修改原始行为。 在 Java 中,实现装饰器设计模式的步骤如下: 定义一个接口或抽象类作为被装饰对象的基类。 公共接口 Component { void operation; } } 在本例中,我们定义了一个名为 Component 的接口,该接口包含一个名为 operation 的抽象方法,该方法定义了被装饰对象的基本行为。 定义一个实现基类方法的具体装饰对象。 公共类 ConcreteComponent 实现 Component { public class ConcreteComponent implements Component { @Override public void operation { System.out.println("ConcreteComponent is doing something...") ; } } 定义一个抽象装饰器类,该类继承于基类,并将装饰对象作为一个属性。 公共抽象类装饰器实现组件 { protected Component 组件 public Decorator(Component component) { this.component = component; } } @Override public void operation { component.operation; } } } 在这个示例中,我们定义了一个名为 Decorator 的抽象类,它继承了 Component 接口,并将被装饰对象作为一个属性。在操作方法中,我们调用了被装饰对象上的同名方法。 定义一个具体的装饰器类,继承自抽象装饰器类并实现增强逻辑。 公共类 ConcreteDecoratorA extends Decorator { public ConcreteDecoratorA(Component 组件) { super(component); } } public void operation { super.operation System.out.println("ConcreteDecoratorA 正在添加新行为......") ; } } 在本例中,我们定义了一个名为 ConcreteDecoratorA 的具体装饰器类,它继承自装饰器抽象类,并实现了操作方法的增强逻辑。在操作方法中,我们首先调用被装饰对象上的同名方法,然后添加新行为。 使用装饰器增强被装饰对象。 公共类 Main { public static void main(String args) { Component 组件 = new ConcreteComponent; component = new ConcreteDecoratorA(component); 组件操作 } } 在这个示例中,我们首先创建了一个被装饰对象 ConcreteComponent,然后通过 ConcreteDecoratorA 类创建了一个装饰器,并将被装饰对象作为参数传递。最后,调用装饰器的操作方法,实现对被装饰对象的增强。 使用场景 在 Java 中,装饰器模式被广泛使用,尤其是在 I/O 中。Java 中的 I/O 库使用装饰器模式实现了不同数据流之间的转换和增强。 让我们打开文件 a.txt,从中读取数据。InputStream 是一个抽象类,FileInputStream 是专门用于读取文件流的子类。BufferedInputStream 是一个支持缓存的数据读取类,可以提高数据读取的效率,具体代码如下: @Test public void testIO throws Exception { InputStream inputStream = new FileInputStream("C:/bbb/a.txt"); // 实现包装 inputStream = new BufferedInputStream(inputStream); byte bytes = new byte[1024]; int len; while((len = inputStream.read(bytes)) != -1){ System.out.println(new String(bytes, 0, len)); } } } } 其中 BufferedInputStream 对读取数据进行了增强。 这样看来,装饰器设计模式和代理模式似乎有点相似,接下来让我们讨论一下它们之间的区别。 第三,与代理模式的区别: 代理模式的目的是控制对对象的访问,它在对象外部提供一个代理对象来控制对原对象的访问。代理对象和原始对象通常实现相同的接口或继承相同的类,以确保两者可以相互替换。 装饰器模式的目的是动态增强对象的功能,而这是通过对象内部的包装器来实现的。在装饰器模式中,装饰器类和被装饰对象通常实现相同的接口或继承自相同的类,以确保两者可以相互替代。装饰器模式也被称为封装器模式。 在代理模式中,代理类附加了与原类无关的功能。
-
Hadoop 大数据架构和关键组件
-
大数据|Hadoop 简介及三大核心组件的两大功能 (II)
-
盘点 Hadoop 生态系统中的 6 个核心大数据组件
-
接口组件 DevExpress WinForms v23.2 - 全新升级的数据展示和用户界面模板功能
-
openEuler郑州用户组成立!openEuler与hyperfusion携手共建河南地区用户生态 - 开幕致辞 超融合操作系统业务总经理、openEuler委员会成员蒋振华先生为本次活动致辞。 在本次活动的致辞中,他提到,作为openEuler社区早期的成员,超融合见证了openEuler从成立到在各行业商业落地,再到跨越生态拐点的过程,感谢openEuler提供了一个全产业链共同创新的平台,共同推动创新技术的商业落地。 同时,本次活动得到了郑州市郑东新区大数据管理局、郑州中原科技城投资服务局的大力支持。 郑东新区大数据管理局曹光远 在活动致辞中表示,openEuler的应用和*应用设施的深度优化,为郑东新区数字化转型提供了安全、可靠、高性能的技术基础;郑州中原科技城招商服务局王林表示,郑东新区欢迎所有openEuler生态相关企业扎根当地,围绕openEuler社区共同发展,形成合力。 openEuler社区及运维功能介绍 openEuler技术委员会委员胡峰 openEuler技术委员会委员胡峰先生在本次活动中介绍了openEuler社区目前发展的整体情况,并重点从技术层面介绍了openEuler的运维功能。 openEuler 晚会 胡峰先生介绍智能运维工具 A-Ops 和 openEuler gala、 阿波罗 Apollo、智能漏洞管理解决方案等新功能,以及涵盖各种运维场景的精品运维组件。在*交流环节,许多用户就目前使用的 openEuler 在*交流环节,许多用户就自己在使用openEuler过程中遇到的一些问题与胡峰先生进行了进一步的交流。 软硬结合,构建多样化算力操作系统 Hyperfusion 基于 openEuler 的基础上,结合自身软硬件技术积累,推出了富讯服务器操作系统 FusionOS FusionOS. FusionOS 首席架构师张海亮 分享了 FusionOS FusionOS首席架构师张海亮分享了FusionOS的软硬件协同优势、卓越的性能和可靠性,以及FusionOS在金融、运营商、*、互联网等行业的实践案例,引起了众多用户的兴趣,分享结束后,不少参会者就FusionOS的特点向讲师提问并进行了交流。
-
在 vue3 中,点击父组件会刷新子组件的数据(刷新父组件中的子组件)