组件通讯

组件通讯

组件通讯主要分为三种:子传父 ,父传子,兄弟通讯

父传子

父组件是通过props属性给子组件通信
props可以传字符串,对象,数组
代码如下

<template>
  <div class="hello">
    <child :inputValue = 'value'></child>
  </div>
</template>
<script>
import child from './child'
export default {
  data () {
    return {
      value:'我是父组件的value'
    }
  },
  components:{child}
}
</script>

子组件

    <template>
       <div>
          <span>{{inputValue}}</span>
       </div>
    </template>
    <script>
    export default {
       props:{
           inputValue:String
       }
       }

子传父

vue子传父使用$emit传值

代码如下

子组件

<template>
    <div>
        <button @click="toParent">点击传到父级</button>
    </div>
</template>
<script>
export default {
    name: 'child',
    methods: {
        toParent () {
            this.$emit('fromChild', 'child')
        }
    }
}
</script>

父组件

<template>
    <div>
        <p>子级传过来的值:{{childVal}}</p>
        <child @fromChild="getChild"></child>
    </div>
</template>
<script>
import child from "@/components/child";
 
export default {
    name: 'parent',
    data () {
        return {
            childVal: ''
        }
    },
    components: {
        child
    },
    methods: {
        getChild (v) {
            this.childVal = v;
        }
    }
}
</script>

兄弟组件通讯

 vue兄弟组件之间的传参是需要,建立一个公共的eventBus,然后兄弟之间以这个为桥梁实现通信
 用$bus.$on接收数据
   $bus.$emit发送数据
   $bus.$off取消监听数据
 具体代码如下
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <title>兄弟组件之间的传参</title>
</head>

<body>
    <div id="app">
        <tom-vue></tom-vue>
        <jerry-vue></jerry-vue>
        <button @click="handle">销毁事件</button>
    </div>
</body>
<script>
    var  hub = new Vue()
    // 兄弟组件A
    var tomVue = {
        data(){
            return {
                num:1
            }
        },
        template:'<button @click ="handle">tom点击了{{num}}次</button>',
        created(){
            // 监听组件的数据
            hub.$on('tom-event',(val) => {
                    this.num += val
                })
        },
        methods:{
            // 触发兄弟组件的事件和给兄弟组件的值
            handle () {
                hub.$emit('jerry-event', 3)
            }
        }
    }
    // 兄弟组件B
    var jerryVue = {
        data(){
            return {
                num:1
            }
        },
        template:'<button @click ="handle">jerry点击了{{num}}次</button>',
        created(){
            // 监听组件的数据
            hub.$on('jerry-event',(val) => {
                    this.num += val
                })
        },
        methods:{
            // 触发兄弟组件的事件和给兄弟组件的值
            handle () {
                hub.$emit('tom-event', 2)
            }
        }
    }
    var vue = new Vue({
        el: '#app',
        data() {
            return {
            }
        },
        components:{
            tomVue,
            jerryVue
        },
        methods:{
            handle(){
                hub.$off('tom-event')
                hub.$off('jerry-event')
            }
        }
    })
</script>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值