随笔 - 115  文章 - 0  评论 - 0  阅读 - 40316

11:vue3 组件传递数据 Props(父传子)

1、父子组件通过Props传递数据,数据可以为固定数据,也可以多个数据,也可以动态数据

新建Parent.vue组件

复制代码
 1 <template>
 2     <h3>Parent</h3>
 3     <Child gd_title="Parent传固定数据" gd_title2="传递第二个参数" :dt_title="message"></Child>
 4 </template>
 5 
 6 <script>
 7 import Child from './Child.vue';
 8 export default{
 9     data(){
10         return{
11             message:"传递动态参数"
12         }
13     },
14     components:{
15         Child
16     }
17 }
18 </script>
复制代码

新建Child.vue组件

复制代码
 1 <template>
 2     <h3>Child</h3>
 3     <p>{{ gd_title }}</p>
 4     <p>{{ gd_title2 }}</p>
 5     <p>{{ dt_title }}</p>
 6 </template>
 7 
 8 <script>
 9 export default{
10     data(){
11         return{
12 
13         }
14     },
15     props:["gd_title","gd_title2","dt_title"]
16 }
17 </script>
复制代码

App.vue引用Parent

复制代码
 1 <template>
 2 <!-- 第三步:显示组件 -->
 3 <Parent></Parent>
 4 </template>
 5 
 6 <script>
 7 //第一步:引入组件
 8 import Parent from './components/Parent.vue';
 9 //第二步:注入组件
10 export default{
11   components:{
12     Parent
13   }
14 }
15 </script>
复制代码

注意事项:传递参数 只能父传子,不能子传父

2、组件传递多种数据类型

在上述的两个例子中,我们只传入了字符串值,但实际上任何类型的值都可以作为 props 的值被传递。

Number

<!-- 虽然 `20` 是个常量,但是我们还是需要使用 v-bind ,因为这是一个 JavaScript 表达式而不是一个字符串 -->,所以采用动态绑定才可

Boolean

同Number一样,也是需要使用 v-bind赋值

复制代码
 1 Parent.vue 代码
 2 
 3 <template>
 4     <h3>----------Parent---------</h3>
 5     <Child gd_title="我是固定的String" :dt_title="message" :age="age"
 6     :isPublished="isPublished" :names="names" :userInfo="userInfo"></Child>
 7 </template>
 8 
 9 <script>
10 import Child from './Child.vue';
11 export default{
12     data(){
13         return{
14             //动态传String
15             message:"传递动态参数",
16             //动态传Number
17             age:20,
18             //动态传Boolean
19             isPublished:true,
20             //Array
21             names:["iwen","ime","frank"],
22             //Object
23             userInfo:{
24                 name:"Tom",
25                 age:24
26             }   
27         }
28     },
29     components:{
30         Child
31     }
32 }
33 </script>
复制代码
复制代码
 1 Child.vue代码
 2 
 3 <template>
 4     <h3>-------Child-----------</h3>
 5     <p>1、固定String:{{ gd_title }}</p>
 6     <p>2、动态String:{{ dt_title }}</p>
 7     <p>3、Number:{{ age }}</p>
 8     <p>4、Boolean:{{ isPublished }}</p>
 9     <p>5、数组Array:</p>
10     <ul>
11         <li v-for="(item,index) of names" :key="index">{{ item }}</li>
12     </ul>
13     <p>6、对象Object:{{ userInfo.name }} -- {{ userInfo.age }}-->{{ userInfo }}</p>
14 </template>
15 
16 <script>
17 export default{
18     data(){
19         return{
20 
21         }
22     },
23     props:["gd_title","dt_title","age","isPublished","names","userInfo"]
24 }
25 </script>
复制代码

 

3、Prop 校验

在子组件接收参数时,加上接收的类型type

复制代码
 1 export default {
 2   props: {
 3     // 基础类型检查
 4     //(给出 `null` 和 `undefined` 值则会跳过任何类型检查)
 5     propA: Number,
 6     // 多种可能的类型
 7     propB: [String, Number],
 8     // 必传,且为 String 类型
 9     propC: {
10       type: String,
11       required: true
12     },
13     // Number 类型的默认值
14     propD: {
15       type: Number,
16       default: 100
17     },
18     // 对象类型的默认值
19     propE: {
20       type: Object,
21       // 对象或者数组应当用工厂函数返回。
22       // 工厂函数会收到组件所接收的原始 props
23       // 作为参数
24       default(rawProps) {
25         return { message: 'hello' }
26       }
27     },
28     // 自定义类型校验函数
29     propF: {
30       validator(value) {
31         // The value must match one of these strings
32         return ['success', 'warning', 'danger'].includes(value)
33       }
34     },
35     // 函数类型的默认值
36     propG: {
37       type: Function,
38       // 不像对象或数组的默认,这不是一个
39       // 工厂函数。这会是一个用来作为默认值的函数
40       default() {
41         return 'Default function'
42       }
43     }
44   }
45 }
复制代码

演示示例代码如下:

复制代码
 1 <script>
 2 export default{
 3     data(){
 4         return{
5 } 6 }, 7 // props:["gd_title","dt_title","age","isPublished","names","userInfo"] 8 props:{ 9 10 gd_title:String, 11 dt_title:String, 12 age:{ 13 type: Number, 14 default:100 15 }, 16 isPublished:Boolean, 17 names:Array, 18 userInfo:Object 19 } 20 } 21 </script>
复制代码

 

posted on   wuzx-blog  阅读(1172)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
历史上的今天:
2017-07-05 取重复记录中时间最新的一条记录Oracle sql语句
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示