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>
  • 13
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue中,父组件向子组件传递参数有多种方式。其中一种方式是通过props属性来传递参数。在父组件中,可以在子组件的标签上使用冒号加上要传递的属性名来绑定一个值,这个值可以是父组件中的数据或者是一个方法。在子组件中,可以通过props来接收父组件传递过来的参数。 如果要实现子组件向父组件传递参数,可以使用$emit方法。在子组件中,可以通过在方法中调用$emit方法来触发一个自定义事件,并将要传递的参数作为第二个参数传入。在父组件中,可以在子组件的标签上使用@符号来监听这个自定义事件,并在相应的方法中接收子组件传递过来的参数。 综上所述,通过props属性和$emit方法,可以实现Vue父子组件之间的参数传递。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue父子组件传参的4种方式](https://blog.csdn.net/glorydx/article/details/112247747)[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_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue3父子组件传参(setup语法糖写法)](https://blog.csdn.net/skyblue_afan/article/details/126667586)[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_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

五秒法则

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

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

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

打赏作者

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

抵扣说明:

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

余额充值