vue3组件传参(父子组件、兄弟组件、爷孙组件)

2 篇文章 0 订阅

目录

一、父传子

1、父组件FatherView.vue

2、子组件SonView.vue

二、子传父

1、子组件SonView.vue

2、父组件FatherView.vue

三、兄弟传参

1、兄弟组件2:BrotherView.vue

2、父组件【中间件】FatherView.vue

3、兄弟组件1:SonView.vue

四、爷传孙

1、爷组件GrandFatherView.vue

2、父组件FatherView.vue

3、孙组件SonView.vue

五、孙传爷

1、爷组件

2、父组件【同上第四点,不变】

3、孙组件SonView.vue


一、父传子

①父组件准备参数

②父子连接点【父组件的子标签】

③子组件props接收数据

注意:在setup语法糖中,defineProps无需引入即可使用

1、父组件FatherView.vue

<script setup lang="ts">
import Son from '@/views/SonView.vue'
</script>

<template>
  <div>
    <Son title="基本信息"></Son>
  </div>
</template>

2、子组件SonView.vue

<script setup lang="ts">
const props = defineProps({
  title: {
    type: String
  }
});
</script>

<template>
  <div>
    <div>{{ props.title }}</div>
  </div>
</template>

二、子传父

①子组件准备事件
注意:在setup语法糖中,defineEmits无需引入即可使用
②父子连接点【父组件的子标签】
格式:@事件='父组件自定义事件名'
③父组件使用函数,函数默认返回一个参数val,即子组件参数

1、子组件SonView.vue

<script setup lang="ts">
const emits = defineEmits(['click'])
const toFather = () => {
  emits('click','我是子组件')
}
</script>

<template>
  <div>
    <div @click="toFather">子组件</div>
  </div>
</template>

2、父组件FatherView.vue

<script setup lang="ts">
import Son from '@/views/SonView.vue'

const getSon = (val:any) => {
  console.log(val);
}
</script>
<template>
  <div>
    <Son @click="getSon"></Son>
  </div>
</template>

三、兄弟传参

1、兄弟组件2:BrotherView.vue

<script setup lang="ts">
const emits = defineEmits(['fromBrother'])
const toSon = () => {
  emits('fromBrother','我有兄弟给的title啦')
}
</script>

<template>
  <div>
    <div @click="toSon">给兄弟一个title</div>
  </div>
</template>

2、父组件【中间件】FatherView.vue

<script setup lang="ts">
import Son from '@/views/SonView.vue'
import Brother from '@/views/BrotherView.vue'
import { ref } from 'vue';

const title = ref('')
const getBrother = (val:any) => {
  title.value = val
}
</script>
<template>
  <div>
    <Brother @fromBrother="getBrother"></Brother>
    <Son :title="title"></Son>
  </div>
</template>

3、兄弟组件1:SonView.vue

<script setup lang="ts">
const props = defineProps({
  title:{
    type:String,
    default:''
  }
})
</script>

<template>
  <div>
    <div>{{ props.title }}</div>
  </div>
</template>

四、爷传孙

①爷组件准备参数,provide(),定义好参数名

②父组件,连接爷孙组件

③孙组件inject接收参数

1、爷组件GrandFatherView.vue

<script setup lang="ts">
import Father from '@/views/FatherView.vue'
import { provide,ref } from 'vue';
provide('id',ref(666))
</script>

<template>
  <div>
    <Father></Father>
  </div>
</template>

2、父组件FatherView.vue

<script setup lang="ts">
import Son from '@/views/SonView.vue'
</script>
<template>
  <div>
    <Son></Son>
  </div>
</template>

3、孙组件SonView.vue

<script setup lang="ts">
import { inject } from 'vue';
const userId = inject('id')
</script>

<template>
  <div>
    <div>{{ userId }}</div>
  </div>
</template>

五、孙传爷

①爷组件provide,写好接收孙组件参数的方法

②父组件,连接爷孙组件

③孙组件inject,对应爷组件方法

1、爷组件

<script setup lang="ts">
import Father from '@/views/FatherView.vue'
import { provide } from 'vue';

const sendMoney:any = (val:any) => {
  console.log(val);
}
provide('sendMoney',sendMoney)
</script>

<template>
  <div>
    <Father></Father>
  </div>
</template>

2、父组件【同上第四点,不变】

3、孙组件SonView.vue

<script setup lang="ts">
import { inject } from 'vue';
const sendMoney = inject('sendMoney')
const toGrand = () => {
  sendMoney(3000)
}
</script>

<template>
  <div>
    <div @click="toGrand">给爷爷点钱</div>
  </div>
</template>
Vue3中,父组件向孙组件通信可以通过使用Provide/Inject这对API来实现。在父组件中,通过使用provide方法提供数据,然后在孙组件中使用inject方法来接收这些数据。这种方式可以跨越多层组件传递数据,实现父孙组件之间的通信。这样可以在不直接通过props和$emit来传递数据的情况下,实现组件之间的数据共享。具体的使用方法可以参考Vue3的官方文档。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue组件通信详解(父子组件, 爷孙组件, 兄弟组件)](https://download.csdn.net/download/weixin_38736760/13128096)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Vue中父组件向子组件通信的方法](https://download.csdn.net/download/weixin_38743054/12777066)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Vue 之孙组件向爷组件通信的实现](https://download.csdn.net/download/weixin_38713996/12943554)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

五秒法则

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值