vue3组件通信--props

news/2024/10/27 14:08:56/

目录

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

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

1.父传子

父组件(FatherComponent.vue):

<script setup>javascript">
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>javascript">
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>javascript">
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>javascript">
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>javascript">
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.ppmy.cn/news/1542358.html

相关文章

五种Pandas图表美化样式汇总

Pandas是一种高效的数据处理库&#xff0c;它以dataframe和series为基本数据类型&#xff0c;呈现出类似excel的二维数据。 在Jupyter中&#xff0c;会美化Pandas的输出。不同于IDE展示的文本形式&#xff0c;Jupyter可以通过CSS修改表格的样式。 我们在做excel表格的时候&am…

Qt中使用线程之QThread

使用Qt中自带的线程类QThread时 1、需要定义一个子类继承自QThread 2、重写run()方法&#xff0c;在run方法中编写业务逻辑 3、子类支持信号槽 4、子类的构造函数的执行是在主线程进行的&#xff0c;而run方法的执行是在子线程中进行的 常用方法 静态方法 获取线程id 可…

【Python】Whoosh:全流程自建搜索引擎

Whoosh是一个纯Python编写的全文搜索库&#xff0c;适用于快速构建搜索引擎。 环境部署 在开始使用Whoosh之前&#xff0c;你需要确保你的开发环境已经正确设置。以下是详细的环境部署步骤。 安装Python 首先&#xff0c;确保你的系统上安装了Python。Whoosh支持Python 2.7…

第二章 Vue之插值表达式

目录 一、引言 二、演示效果 三、完整代码 一、引言 插值表达式 {{ }} 是一种Vue的模板语法 语法&#xff1a;{{ 表达式 }} 作用&#xff1a;利用表达式进行插值&#xff0c;渲染到页面中。 注意&#xff1a; 1. 表达式是可以被求值的代码&#xff0c;JS引擎会将其计算出一个…

Java面试题——微服务篇

1.微服务的拆分原则/怎么样才算一个有效拆分 单一职责原则&#xff1a;每个微服务应该具有单一的责任。这意味着每个服务只关注于完成一项功能&#xff0c;并且该功能应该是独立且完整的。最小化通信&#xff1a;尽量减少服务之间的通信&#xff0c;服务间通信越少&#xff0c…

Linux 中 .bash_history、.bash_logout 等用户配置文件

目录 前言.bash_history.bash_logout.bash_profile.bashrc.cshrc.tcshrc.viminfo 总结 前言 在 Linux 中我们经常会看见用户家目录下存在 .bash_history、.bash_logout、.bash_profile、.bashrc、.cshrc、.tcshrc、.viminfo 这写文件&#xff0c;那它们区别是什么呢&#xff1…

【数据结构】贪心算法:决策的艺术

贪心算法&#xff08;Greedy Algorithm&#xff09;是一类在每一步选择中都采取局部最优解的方法&#xff0c;希望最终能够达到全局最优解。通俗地说&#xff0c;贪心算法的思想就是“每一步都尽量做出最好的选择”&#xff0c;以期望整个过程的最终结果也达到最优状态。贪心算…

出处不详 凸多边形最优三角剖分

目录 出处不详 凸多边形最优三角剖分题目描述背景输入输出数据范围 题解 出处不详 凸多边形最优三角剖分 题目描述 背景 给定 n n n边凸多边形&#xff0c;要求确定该凸多边形的三角剖分&#xff08;将多边形分割成 n − 2 n - 2 n−2个三角形&#xff09;&#xff0c;使得该…