全局挂载事件总线,实现兄弟、父子通信。

效果

  1. dad 发送消息
    在这里插入图片描述
  2. son发送消息
    在这里插入图片描述

步骤

  1. 在 main.js 挂载事件总线 Bus
  2. $emit(‘事件名’,‘值’)
  3. $on(‘事件名’,value =>{ 接收value })

方法

方法1

  1. main.js
Vue.prototype.bus = new Vue()
  1. 父组件
<template>
  <div class="dad">
    <input type="text " v-model="dad" />
    <button @click="sendMsg">dad发送消息</button>
    <div>接收son消息:{{son}}</div>
    <son></son>
    <girl></girl>
  </div>
</template>

<script>
import son from './son.vue'
import girl from './girl.vue'

export default {
  components: {
    son,
    girl
  },
  mounted() {
    this.bus.$on('child', value => {
      this.son = value
    })
  },
  data() {
    return {
      dad: '', // 父组件发送的信息
      son: '' // 儿子发送的信息
    }
  },
  methods: {
    sendMsg() {
      this.bus.$emit('parent', this.dad)
    }
  }
}
</script>

<style>
.dad {
  background-color: antiquewhite;
  width: 500px;
  height: 500px;
  transform: translate(50%);
}
</style>
  1. son组件
<template>
  <div>
    <div class="box">
      <h4>接收dad消息:{{dad}}</h4>
      <input type="text" v-model="son">
      <button @click="sendMsg">son发送消息</button>
    </div>
  </div>
</template>

<script>
// import bus from './event'
export default {
  data() {
    return {
      dad: '默认',
      son:''
    }
  },
  mounted() {
    this.bus.$on('parent', value => {
      this.dad = value
    })
  },
  methods:{
    sendMsg(){
      this.bus.$emit('child',this.son)
    }
  }
}
</script>

<style>
.box {
  background-color: pink;
  height: 200px;
  width: 200px;
}
</style>
  1. girl组件
<template>
  <div>
    <div class="girl">
      <h4>接收dad消息:{{dad}}</h4>
      <h4>接收son消息:{{son}}</h4>
    </div>

  </div>
</template>

<script>
// import bus from './event'
export default {
  data() {
    return {
      dad: '',
      son:''
    }
  },
  mounted() {
    this.bus.$on('parent', value => {
      this.dad = value
    })

     this.bus.$on('child', value => {
      this.son = value
    })
  }
}
</script>

<style>
.girl {
  background-color: rgb(206, 234, 245);
  width: 200px;
  height: 200px;
}
</style>

方法2

只是挂载方法不同,所以只放 main.js 部分,其他部分见方法1。

new Vue({
  render: h => h(app),
  beforeCreate () {
    // 安装全局事件总线
    Vue.prototype.bus = this
  }
}).$mount('#app')

方法3

此方法没有全局挂载,但挂载方式不同,且使用时不需加 this 。
所以此处只放 dad 页面,以示 bus 使用方法。

  1. 新建event.js
import Vue from 'vue'
export default new Vue()
  1. dad
    1)导入 import bus from './event'
    2)使用 bus.$onbus.$emit
<template>
  <div class="dad">
    <input type="text " v-model="dad" />
    <button @click="sendMsg">dad发送消息</button>
    <div>接收son消息:{{son}}</div>
    <son></son>
    <girl></girl>
  </div>
</template>

<script>
import son from './son.vue'
import girl from './girl.vue'
import bus from './event'

export default {
  components: {
    son,
    girl
  },
  mounted() {
    bus.$on('child', value => {
      this.son = value
    })
  },
  data() {
    return {
      dad: '', // 父组件发送的信息
      son: '' // 儿子发送的信息
    }
  },
  methods: {
    sendMsg() {
      bus.$emit('parent', this.dad)
    }
  }
}
</script>

<style>
.dad {
  background-color: antiquewhite;
  width: 500px;
  height: 500px;
  transform: translate(50%);
}
</style>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值