Vue组件间通信方式超详细(父传子、父传后代、子传父、后代传父、兄弟组件传值)

一、父传子、父传后代

方式一:子通过props来接收

image.png
父组件:父组件引入子组件时,通过<child :parentValue = "parentValue"></child>子组件传值。

备注:这种方式父传值很方便,但是传递给后代组件不推荐(父->子->孙),且这种方式父组件不能直接修改父组件传过来的数据。

<template>
  <div>
    <h1>父组件</h1>
    <child :parentValue = "parentValue"></child>
  </div>
</template>

<script>
  import Child from "./child";
  export default {
    name: 'parent',
    components: {Child},
    data () {
      return {
        parentValue:"父组件内的值"
      }
    }
  }
</script>
<style scoped>

</style>

子组件:子组件通过props即props:{ parentValue:{ type:String, default:"" } }来接收父组件传过来的值

<template>
  <div>
    <h2>子组件</h2>
    <hr>
    <span>{{parentValue}}</span>
  </div>
</template>

<script>
  export default {
    name: 'child',
    props:{
      parentValue:{
        type:String,
        default:""
      }
    },
    data () {
      return {
      }
    },
  }
</script>
<style scoped>

</style>

方式二:通过this.$parent.xxx子组件直接使用父组件的值

备注:这种方式,子组件可以直接修改父组件的数据。

image.png
父组件:正常引入子组件

<template>
  <div>
    <h1>父组件</h1>
    <child></child>
  </div>
</template>

<script>
  import Child from "./child";
  export default {
    name: 'parent',
    components: {Child},
    data () {
      return {
        parentValue:"父组件内的值"
      }
    }
  }
</script>

子组件:通过this.$parent.parentValue获取父组件的数据。

<template>
  <div>
    <h2>子组件</h2>
    <span>我是通过this.$parent.xxx直接获取父组件的值:</span>
    <br>
<!--    <span>{{this.$parent.parentValue}}</span>-->
    <span>{{parentValueToSon}}</span>
  </div>
</template>

<script>
  export default {
    name: 'child',
    data () {
      return {
        parentValueToSon:"",
      }
    },
    created() {
      this.parentValueToSon = this.$parent.parentValue;
    }
  }
</script>

方式三:依赖注入provide/inject

备注:这种方式父组件可以直接向某个后代组件传值,不用再一级一级的传递。

缺点:很难去找这个值是从哪个组件传递过来的。

image.png
父组件:通过provide定义需要传递给后代的数据。

<template>
  <div>
    <h1>父组件</h1>
    <hr>
    <child></child>
  </div>
</template>

<script>
  import Child from "./child";
  export default {
    name: 'parent',
    components: {Child},
    data () {
      return {
      }
    },
    //通过依赖注入方式传递给后代的数据
    provide(){
      return{
        parentProvideValue:"依赖的父组件的值"
      }
    }
  }
</script>

子组件:

<template>
  <div>
    <h2>子组件</h2>
    <span>我是通过this.$parent.xxx直接获取父组件的值:</span>
    <br>
    <!--<span>{{this.$parent.parentValue}}</span>-->
    <span>{{parentValueToSon}}</span>
    <hr>
    <grandson></grandson>
  </div>
</template>

孙子组件:通过inject注入爷爷辈组件传递过来的值。

<template>
  <div>
    <h3>孙子组件</h3>
    <span>获取到的爷爷辈组件传递过来的值:</span>
    <br>
    <span>{{parentProvideValue}}</span>
  </div>
</template>

<script>
  export default {
    name: 'grandson',
    //获取父组件传递过来的值
    inject:['parentProvideValue'],
    data () {
      return {
      }
    }
  }
</script>
<style scoped>

</style>

效果图:

image.png

二、子传父、后代传父

方式一:this.$emit(“function”,param)

子组件通过$emit传递一个函数和参数,父组件通过传递过来的函数接收参数即传过来的值。

父子组件一般会触发交互行为(子组件传递过来的值放在生命周期函数里是传不过来的),所以可以通过父子的交互行为获取到子组件传递过来的数据。

image.png
父组件:通过子组件自定义的函数进行绑定接收传递过来的数据。

<template>
  <div>
    <h1>父组件</h1>
    <span>接收到子组件传递过来的值:</span>
    <span>{{getSonToParentValue}}</span>
    <hr>
    <child @tansToParent = "tansToParent"></child>
  </div>
</template>

<script>
  import Child from "./child";
  export default {
    name: 'parent',
    components: {Child},
    data () {
      return {
        getSonToParentValue:"",
      }
    },
    mounted() {
    },
    methods:{
      tansToParent(val) {
        this.getSonToParentValue = val;
        console.log("子组件传递过来的值",val)
      }
    }
  }
</script>

子组件:通过this. e m i t ( " f u n c t i o n " , p a r a m ) 子组件通过 emit("function",param) 子组件通过 emit("function",param)子组件通过emit传递一个函数和参数,父组件通过传递过来的函数接收参数即传过来的值。

<template>
  <div>
    <h2>子组件</h2>
    <button @click="toParentValue">子组件按钮</button>
    <hr>
    <grandson></grandson>
  </div>
</template>

<script>
  import Grandson from "./grandson";
  export default {
    name: 'child',
    components: {Grandson},
    data () {
      return {
        childValue:"子组件传递给父组件的值",
      }
    },
    created() {

    },
    methods:{
      toParentValue(){
        //通过this.$emit给父组件传值
        this.$emit('tansToParent',this.childValue)
      }
    }
  }
</script>

效果图:

image.png

方式二:this.$child.xxx直接获取子组件数据,且可直接修改子组件的数据。

父组件:this.$children[0].childValue获取子组件数据,只有一个子组件故下标为0.

<template>
  <div>
    <h1>父组件</h1>
    <span>接收到子组件传递过来的值:</span>
    <span>{{getSonToParentValue}}</span>
    <hr>
    <child></child>
  </div>
</template>

<script>
  import Child from "./child";
  export default {
    name: 'parent',
    components: {Child},
    data () {
      return {
        getSonToParentValue:"",
      }
    },
    mounted() {
      this.getSonToParentValue = this.$children[0].childValue
    }
  }
</script>

子组件:

<template>
  <div>
    <h2>子组件</h2>
    <button>子组件按钮</button>
    <hr>
    <grandson></grandson>
  </div>
</template>

<script>
  import Grandson from "./grandson";
  export default {
    name: 'child',
    components: {Grandson},
    data () {
      return {
        childValue:"子组件传递给父组件的值",
      }
    },
    created() {

    },
    methods:{
    }
  }
</script>

方式三:通过ref/refs获取子组件dom从而直接获取子组件数据。可直接修改子组件数据。

父组件:

<template>
  <div>
    <h1>父组件</h1>
    <span>接收到子组件传递过来的值:</span>
    <span>{{getSonToParentValue}}</span>
    <hr>
    <child ref="childDom"></child>
  </div>
</template>

<script>
  import Child from "./child";
  export default {
    name: 'parent',
    components: {Child},
    data () {
      return {
        getSonToParentValue:"",
      }
    },
    mounted() {
      this.getSonToParentValue = this.$refs.childDom.childValue
    }
  }
</script>

子组件与上述相同。

三、兄弟组件传值

方式一:通过中转eventBus.js工具类

新建一个中转eventBus.js工具类,传值的兄弟组件自定义一个函数通过

eventBus.$emit('function',参数);

给接收值的兄弟组件传一个约定好的function名称及参数(即传递的值);接收值的兄弟组件通过

eventBus.$on('function',val=>{
  console.log("传递过来的值",val)
})

来接收传递过来的值。

image.png
eventBus.js:

import Vue from 'vue'
export default new Vue();

传值的兄弟组件:

<template>
  <div>
    <h2>子组件</h2>
    <button @click="toBrother">点击给兄弟组件传值</button>
  </div>
</template>

<script>
  import Grandson from "./grandson";
  import eventBus from "../utills/eventBus";
  export default {
    name: 'child',
    components: {Grandson},
    data () {
      return {
        transToBrother:"这是传递给兄弟组件的值",
      }
    },
    methods:{
      toBrother(){
        eventBus.$emit('toBrotherFunc',this.transToBrother);
      }
    }
  }
</script>

接收值的兄弟组件:

<template>
  <div>
    <h3>孙子组件</h3>
    <span>兄弟组件传递过来的值:</span>
    <span>{{eventBusValue}}</span>
  </div>
</template>

<script>
  import eventBus from "../utills/eventBus";
  export default {
    name: 'grandson',

    data () {
      return {
        eventBusValue:"",
      }
    },
    created() {
      eventBus.$on('toBrotherFunc',val=>{
        this.eventBusValue = val;
      })
    }
  }
</script>

效果图:
在这里插入图片描述

  • 7
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue中,子组件向父组件传值可以通过使用事件来实现。以下是一种常见的实现方法: 1. 在子组件中,使用`$emit`方法触发一个自定义事件,并传递需要传递给父组件的值作为参数。例如,可以在子组件的某个方法中使用`this.$emit('change', value)`来触发名为`change`的事件,并将`value`作为参数传递给父组件。 2. 在父组件中,通过在子组件的标签上监听相应的事件来接收传递过来的值。例如,可以在父组件的模板中使用`v-on:change="handleValueChange"`来监听名为`change`的事件,并将触发事件时传递的值作为参数传递给名为`handleValueChange`的方法。 3. 在父组件的方法中,可以通过接收到的参数来处理传递过来的值,例如更新父组件的数据或执行其他操作。 总结起来,子组件通过`$emit`方法触发一个自定义事件,父组件通过在子组件标签上监听相应的事件来接收传递过来的值。这样就实现了子组件向父组件传值的功能。 参考文献: Vue 没有直接子对子传参的方法,建议将需要传递数据的子组件,都合并为一个组件。如果一定需要子对子传参,可以先从传到父组件,再传到子组件。 下面小编就为大家分享一篇vue组件向父组件传值方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧。 子组件:// 向父组件传递了两个值 this.$emit('change', this.value,this.text); 请注意,以上是一种常见的实现方法,但并不是唯一的方法。在Vue中还有其他方式可以实现子组件向父组件传值的功能,如使用`$parent`属性或通过Vuex进行状态管理等。具体选择哪种方法取决于你的具体需求和项目架构。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值