【vue3】组件通信_props的使用

文章目录


props简介

  1. 父传递子数据+typescript类型检查

父组件代码

<template>
    <!--写html-->
    <div class="container">
        <h1>我的第一个组件</h1>
        <Item :arrData="arrData" :a="testData" :otherData="otherData" />
    </div>
</template>
<script setup lang="ts">
    import Item from "./components/item.vue"
    import {ref} from "vue"
    import {reactive} from "@vue/runtime-core";

    let testData = ref("我修改了")

    let otherData = ref("其他数据")

    let arrData = reactive([
        {
            name:"张三",
            age:16,
            hobby:"打篮球"
        },{
            name:"张四",
            age:17
        },{
            name:"张五",
            age:18
        },
    ])

</script>

子组件代码

<template>
    <div class="itemStyle">
        <div>{{test}}</div>
        <div v-for="(item,index) in test.arrData" :key="index">
            <span>{{item.name}}</span>
            <span>{{item.age}}</span>
            <span>{{item.hobby}}</span>
        </div>
    </div>
</template>

<script setup lang="ts">

    import {defineProps} from "vue";
    
    //vue文件夹中,引入外部类型检测文件,需要把vue版本升级到3.4以上。
    import {type propsInterface} from "@/components/types";

    //可以通过propsInterface类型检查 父传子 的数据
    let test = withDefaults(defineProps<propsInterface>(),{
		//如果父组件没有传对应的属性,这里可以写属性的默认值
		arrData:[],
		a:1,
		otherData:""
	})
    
    console.log(test);
</script>

"@/components/types"文件下的ts文件代码

interface arrDataInterface{
    name:string
    age:number
    hobby?:string
}
export interface propsInterface{
    arrData:arrDataInterface[]
    otherData?:string
    a?:string
}

特别注意:vue文件夹中,引入外部类型检测文件,需要把vue版本升级到3.4以上。旧版本vue不支持ts使用外部文件引入

  1. 子传递父数据

子组件代码

<template>
    <div class="itemStyle">
        <div>
            <button @click="handleSonToParent">子传父</button>
        </div>
    </div>
</template>

<script setup lang="ts">

    import {defineProps} from "vue";
    import {type propsInterface} from "@/components/types";
    
    //子传父
    const emit = defineEmits(['childData'])

    const handleSonToParent = ()=>{
        emit("childData",{a:1,b:2})
    }
</script>

父组件代码

<template>
    <!--写html-->
    <div class="container">
        <h1>我的第一个组件</h1>
        <Item @childData="handleChildData" />
    </div>
</template>
<script setup lang="ts">
    import Item from "./components/item.vue"

    const handleChildData = (val)=>{
        console.log("子传父",val);
    }

</script>

踩坑不易,还希望各位大佬支持一下 \textcolor{gray}{踩坑不易,还希望各位大佬支持一下} 踩坑不易,还希望各位大佬支持一下

📃 个人主页: \textcolor{green}{个人主页:} 个人主页: 沉默小管

📃 个人网站: \textcolor{green}{个人网站:} 个人网站: 沉默小管

📃 个人导航网站: \textcolor{green}{个人导航网站:} 个人导航网站: 沉默小管导航网

📃 我的开源项目: \textcolor{green}{我的开源项目:} 我的开源项目: vueCms.cn

🔥 技术交流 Q Q 群: 837051545 \textcolor{green}{技术交流QQ群:837051545} 技术交流QQ群:837051545

👍 点赞,你的认可是我创作的动力! \textcolor{green}{点赞,你的认可是我创作的动力!} 点赞,你的认可是我创作的动力!

⭐️ 收藏,你的青睐是我努力的方向! \textcolor{green}{收藏,你的青睐是我努力的方向!} 收藏,你的青睐是我努力的方向!

✏️ 评论,你的意见是我进步的财富! \textcolor{green}{评论,你的意见是我进步的财富!} 评论,你的意见是我进步的财富!

如果有不懂可以留言,我看到了应该会回复
如有错误,请多多指教

### Vue3使用 Props 实现组件通信的方法 在 Vue3 中,`props` 是一种用于父组件向子组件递数据的有效机制。当定义一个子组件时,可以指定它接收哪些 `props`,以及这些属性的数据类型和默认。 #### 定义并验证 Prop 类型 为了确保入的数据符合预期,在创建子组件时可通过声明 prop 的类型来进行基本的验证[^2]: ```javascript // 子组件 ChildComponent.vue export default { props: { message: String, // 只接受字符串类型的作为message参数 count: Number, isActive: Boolean, items: Array, config: Object, callback: Function, customEvent: Symbol } } ``` #### 设置 Prop 默认 对于某些可选性的配置项,可以在不提供具体数的情况下让其采用预设好的缺省状态。这有助于减少不必要的错误,并提高代码健壮性[^4]: ```javascript // 子组件 DefaultPropsExample.vue export default { props: { title: { type: String, default: &#39;Default Title&#39; }, description: { type: String, required: false, default: () => &#39;&#39; } } }; ``` #### 父级到子级的数据流动 下面是一个简单的例子展示了如何利用 `props` 来完成从父组件到子组件的信息输过程[^1]: ```html <!-- ParentComponent.vue --> <template> <div class="parent"> <!-- 向子组件递名为 "greetingMessage" 的prop --> <child-component :greeting-message="msg"></child-component> </div> </template> <script setup> import { ref } from &#39;vue&#39;; const msg = ref(&#39;Hello from parent!&#39;); </script> ``` ```html <!-- ChildComponent.vue --> <template> <p>{{ greetingMessage }}</p> <!-- 显示来自父组件的消息 --> </template> <script setup> defineProps({ greetingMessage: String }); </script> ``` 在这个案例里,父组件通过绑定带有冒号 (`:`) 前缀的自定义标签属性来发送消息给子组件;而子组件则负责接收并通过模板语法将其渲染出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉默小管

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值