【Vue父子组件间通信】

父子组件通信

在这里插入图片描述

父传子 (props)

(1)在父组件中使用子组件时,给子组件一个自定义属性,这个属性绑定了父组件中的数据
(2)然后在子组件中用props接收自定义的属性
(3)子组件使用父组件数据
//(1)父组件中:
<Son :fatherName="name" :fatherAge="age"/>
//(2)子组件中:
props: ['fatherName','fatherAge']
//(3)子组件中:
<h3>我的父亲是{{fatherName}} 组件</h3>
<h3>我的父亲今年{{fatherAge}} 岁了</h3>

子传父 (自定义事件)

(1)在父组件中给子组件绑定自定义事件,<Son @xxx="func"/>
func是自定义事件被触发后,执行的回调(即自定义事件被触发后,父组件该做些什么)

(2)如何触发自定义事件:子组件做了某些事情后,告诉父组件,我触发了自定义事件,并捎带一些数据
(3)父组件使用子组件数据
//(1)父组件中:
<Son @getFromSon="showSon"/>

showSon(name,age) {
  console.log(`My son's name is ${name}, he is ${age} now!`);
  this.sonName = name
  this.sonAge = age
}
//(2)子组件中:
<button @click="sendToFather">点我传信息给父亲</button>

sendToFather() {
  this.$emit('getFromSon',this.name,this.age)
}
//(3)父组件中:
<h3>我的儿子是{{sonName ? sonName : '不知道'}}</h3>
<h3>我的儿子今年{{sonAge ? sonAge : '不知道多少'}} 岁了</h3>

在这里插入图片描述

完整代码

Father.vue:

<template>
  <div>
      <h3>我是{{name}} 组件</h3>
      <h3>我今年{{age}} 岁了</h3>
      <h3>我的儿子是{{sonName ? sonName : '不知道'}}</h3>
      <h3>我的儿子今年{{sonAge ? sonAge : '不知道多少'}} 岁了</h3>
      <br><br>
      <Son :fatherName="name" :fatherAge="age" @getFromSon="showSon"/>
  </div>
</template>

<script>
  import Son from '@/components/Son.vue'
  export default {
    name: 'Father',
    data() {
      return {
        name: 'AAA',
        age: 32,
        sonName: '',
        sonAge: 0
      }
    },
    components: { Son },
    methods: {
      showSon(name,age) {
        console.log(`My son's name is ${name}, he is ${age} now!`);
        this.sonName = name
        this.sonAge = age
      }
    }
  }
</script>

<style>

</style>

Son.vue:

<template>
  <div>
      <h3>我是{{name}} 组件</h3>
      <h3>我今年{{age}} 岁了</h3>
      <h3>我的父亲是{{fatherName}} 组件</h3>
      <h3>我的父亲今年{{fatherAge}} 岁了</h3>
      <button @click="sendToFather">点我传信息给父亲</button>
  </div>
</template>

<script>
  export default {
    name: 'Son',
    data() {
      return {
        name: 'aaa',
        age: 6
      }
    },
    props: ['fatherName','fatherAge'],
    methods: {
      sendToFather() {
        this.$emit('getFromSon',this.name,this.age)
      }
    }
  }
</script>

<style>

</style>

效果

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3中,父子组件之间的通信可以通过props和emit来实现。 1. 使用props:父组件可以通过props属性向子组件传递数据。在子组件中,可以通过接收props属性来使用这些数据。在父组件中,可以通过修改props属性的值来实现与子组件的通信。 父组件示例: ```vue <template> <div> <child-component :message="parentMessage"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentMessage: 'Hello from parent component' }; } }; </script> ``` 子组件示例: ```vue <template> <div> <p>{{ message }}</p> </div> </template> <script> export default { props: { message: { type: String, required: true } } }; </script> ``` 2. 使用emit:子组件可以通过emit方法触发自定义事件,然后父组件可以通过监听这些事件来获取子组件传递的数据。 父组件示例: ```vue <template> <div> <child-component @child-event="handleChildEvent"></child-component> <p>{{ messageFromChild }}</p> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { messageFromChild: '' }; }, methods: { handleChildEvent(message) { this.messageFromChild = message; } } }; </script> ``` 子组件示例: ```vue <template> <div> <button @click="sendMessageToParent">Send Message to Parent</button> </div> </template> <script> export default { methods: { sendMessageToParent() { this.$emit('child-event', 'Hello from child component'); } } }; </script> ``` 这两种方式都可以实现父子组件之间的通信,你可以根据具体的需求选择使用哪种方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值