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

vue3组件通信--props

目录

  • 1.父传子
  • 2.子传父

最近在做项目的过程中发现,props父子通信忘的差不多了。下面写个笔记复习一下。

1.父传子

父组件(FatherComponent.vue):

<script setup>
import ChildComponent from "@/components/ChildComponent.vue"
import { ref } from "vue"const fatherMoney = ref(1000)
</script><template><div class="bg-blue h-75 w-100 ma-auto"><h1 class="text-center">我是父组件</h1><ChildComponent :money="fatherMoney"></ChildComponent></div>
</template>

我们可以在子组件标签上写:money="fatherMoney"。意思就是把父亲的响应式变量fatherMoney给子组件,子组件在组件内部要用money来接受这个变量。
子组件(ChildComponent.vue):

<script setup>
const props = defineProps(['money','updateMoney'])
</script><template><div class="bg-purple h-50 w-75 ma-auto"><h1 class="text-center">我是子组件</h1><h3>父亲给我的钱:{{money}}元</h3></div>
</template>

子组件<h3>父亲给我的钱:{{money}}元</h3>这一块儿,我们可以用props.money来渲染这个数据,也可以省略props,直接写money

注意,用props来接受的数据是只读的,子组件不能再组件内部更改它。
比如,不能下面这样写,否则控制台会报错:

<script setup>
const props = defineProps(['money'])const updateMoney = () => {props.money = 100
}
</script><template><div class="bg-purple h-50 w-75 ma-auto"><h1 class="text-center">我是子组件</h1><h3>父亲给我的钱:{{money}}元</h3><v-btn @click="updateMoney" class="text-white bg-blue">修改父亲给我的钱</v-btn></div>
</template>

在这里插入图片描述

2.子传父

子组件向父组件发送数据,父组件需要定义一个方法,用来接受子组件发送的数据:
父组件(FatherComponent.vue):

<script setup>
import ChildComponent from "@/components/ChildComponent.vue"
import { ref } from "vue"const fatherMoney = ref(1000)const childToy = ref('')
const getToy = (value)=>{childToy.value = value
}
</script><template><div class="bg-blue h-75 w-100 ma-auto"><h1 class="text-center">我是父组件</h1><h3>儿子给我的玩具:{{childToy}}</h3><ChildComponent :money="fatherMoney" :sendToy="getToy"></ChildComponent></div>
</template>

:sendToy="getToy"意思就是,父组件给子组件传递了一个方法getToy,子组件要用方法sendToy,给父亲发送数据。
子组件(ChildComponent.vue):

<script setup>
import {ref} from "vue"const props = defineProps(['money','sendToy'])const toy = ref('奥特曼')
</script><template><div class="bg-purple h-50 w-75 ma-auto"><h1 class="text-center">我是子组件</h1><h3>父亲给我的钱:{{money}}元</h3><v-btn @click="sendToy(toy)" class="text-white bg-blue">把玩具给父亲</v-btn><h3>儿子的玩具:{{toy}}</h3></div>
</template>

在这里插入图片描述

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

相关文章:

  • leetcode-75-颜色分类
  • 【嵌入式原理设计】实验三:带报警功能的数字电压表设计
  • C#中的接口的使用
  • 记一次真实项目的性能问题诊断、优化(阿里云redis分片带宽限制问题)过程
  • LeetCode - 4. 寻找两个正序数组的中位数
  • 算法设计与分析——动态规划
  • 【实战篇】GEO是什么?还可以定义新的数据类型吗?
  • SpringBoot最佳实践之 - 项目中统一记录正常和异常日志
  • 【Flutter】状态管理:高级状态管理 (Riverpod, BLoC)
  • OAK相机的RGB-D彩色相机去畸变做对齐
  • smartctl硬盘检查工具
  • 清空MySQL数据表
  • 2024年妈杯MathorCup大数据竞赛A题超详细解题思路
  • Kafka系列之:Kafka集群磁盘条带划分和Kafka集群磁盘扩容详细方案
  • 【LeetCode】修炼之路-0007- Reverse Integer (整数反转)【python】
  • 【Flutter】页面布局:线性布局(Row 和 Column)
  • C语言巨难题:执行操作可获得的最大总奖励 I(C语言版)
  • 【力扣】GO解决子序列相关问题
  • Ubuntu20.04安装VM tools并实现主机和虚拟机之间文件夹共享
  • Linux 学习笔记(十七)—— 文件系统
  • 【计算机网络 - 基础问题】每日 3 题(五十八)
  • Netty入门基础:IO模型中BIO\NIO概念及区别【附演示代码】
  • vue2 使用环境变量
  • 数据预处理
  • django宠物领养管理系统-计算机毕业设计源码26858
  • 使用TeamViewer远程局域网内的两台电脑
  • GUI简介、Swing的常用组件、java程序的运行过程、class文件、JAR、runable_jar、双括号初始化
  • @Autowired和@Resource和getBean()区别
  • Merlion笔记(四):添加一个新的预测模型
  • 【论文阅读】ESRGAN