企业官网建站 小程序开发 云逸学习园地
技术文章 许愿墙
技术文章 PHP学习 Javascript学习 DIV+CSS学习 uniapp学习 HTML学习 微信小程序、公众号知识点 VUE知识学习
您所在的位置>云逸小栈>VUE知识学习>正文

VUE3父向子组件直接传值的方法

2025-10-07 12:14:10 浏览 160

子组件

<!-- ChildComponent.vue -->
<template>
 <div>
   <h3>子组件</h3>
   <p>接收到的消息:{{ message }}</p>
   <p>接收到的数字:{{ count }}</p>
 </div></template>
 <script setup>// 定义
 propsconst props = defineProps({  
 message: {    
   type: String,    
   required: true // 可选,根据需求设置
 },  
 count: {    
   type: Number,    
   default: 0 // 可选默认值
 }
});// 在模板中可以直接使用 props.message, props.count
</script>

<!-- ParentComponent.vue --><template>
 <div>
   <h2>父组件</h2>
   <ChildComponent
     :message="'Hello from Parent'"
     :count="42"
   />
 </div>
 </template>
 <script setup>
 import ChildComponent from './ChildComponent.vue'
 </script>


网站开发者电话

18066742510

个人微信号
个人公众号
个人小程序
个人抖音