vue 兄弟组件通信bus/总线程通信/发布订阅模式

习惯用vuex 但如果不依赖store 可以在vue全局绑定bus 的demo

我新建了util/bus.js

import Vue from 'vue'
export default Vue.prototype.bus = new Vue()

在main.js

import bus from './util/bus'

需要发消息的组件

<template>
  <button @click="changeBus()">bus</button>  
</template>
...
methods: {
    changeBus(){
      this.bus.$emit("change",'yjw');
    }
}

接收消息的兄弟组件 

 mounted(){
    let self = this;
    this.bus.$on('change',function(msg){
      console.log(msg);
      self.msg = msg;
    })
  }
原文地址:https://www.cnblogs.com/junwu/p/11205909.html