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

vue3父组件给子组件传值,并在子组件接受

1、在父组件中定义数据: 在父组件中定义需要传递给子组件的数据。

<template><div><ChildComponent :message="parentMessage" /></div>
</template><script>
import { defineComponent } from 'vue';
import ChildComponent from './ChildComponent.vue';export default defineComponent({components: {ChildComponent,},data() {return {parentMessage: 'Hello from parent!',};},
});
</script>

2、在子组件中接收props: 在子组件中使用props来接收父组件传递的数据。

方法一:

<template><div><p>{{ message }}</p></div>
</template><script>
import { defineComponent, PropType } from 'vue';export default defineComponent({props: {message: {type: String as PropType<string>,required: true,},},
});
</script>

方法二:

在这个修改后的代码中,我们使用了 defineProps 来定义 props,而不是在 defineComponent 中。这是因为在 <script setup> 中,Vue 3 已经隐式地为你创建了一个组件。

<script setup lang="ts">
import { defineProps, PropType } from 'vue';const props = defineProps({message: {type: String as PropType<string>,required: true,},
});
</script>

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

相关文章:

  • Python爬虫如何快速入门
  • 酷开科技依托酷开系统用“平台+产品+场景”塑造全屋智能生活!
  • P8649 [蓝桥杯 2017 省 B] k 倍区间:做题笔记
  • LeetCode题练习与总结:旋转图像
  • 如何在家中使用手机平板电脑 公司iStoreOS软路由实现远程桌面
  • 【文献分享】myMUSCLE, a New Multiphysics, Multiscale Simulation Coupling Environment
  • 2024年云计算使用报告,89%组织用多云,25%广泛使用生成式AI,45%需要跨云数据集成,节省成本是云首要因素
  • 【Python操作基础】——序列
  • Vue 与 React:前端框架对比分析
  • 解决kubesphere流水线docker登陆错误http: server gave HTTP response to HTTPS client
  • macOS安装mongoDB(homebrew)
  • 免费SSL证书和付费SSL证书的区别点
  • 【SQL】1633. 各赛事的用户注册率(COUNT函数 表达式用法)
  • 【LVGL-使用SquareLine Studio设计器 】
  • 将二进制数a的每一位右移b位operator.rshift(a,b)
  • M芯片 mac配置Vulkan环境报错 Xcode
  • Day23:事务管理、显示评论、添加评论
  • 第一篇:概述、 目录、适用范围及术语 --- IAB/MRC《增强现实(AR)广告(效果)测量指南1.0 》
  • pytorch常用的模块函数汇总(2)
  • OpenAI奥特曼豪赌1.42亿破解长生不老
  • [晕事]今天做了件晕事29;iptables
  • 2018年亚马逊云科技推出基于Arm的定制芯片实例
  • 用搜索引擎收集信息-常用方式
  • Adobe推出20多个,企业版生成式AI定制、微调服务
  • 叁[3],NavigationDrawerViewsActivity新增Fragment
  • 备考ICA----Istio实验7---故障注入 Fault Injection 实验
  • [flask]异常抛出和捕获异常
  • js逆向之实例某宝热卖(MD5)爬虫
  • 7、jenkins项目构建细节-常用的构建触发器
  • 【前端学习——css篇】4.px和rem的区别