beforeCreate(){ Vue.prototype.$bus = this; }

    1. import Vue from 'vue'
    2. import App from './App.vue'
    3. import ElementUI from 'element-ui';
    4. import Directives from './directives/index'
    5. import 'element-ui/lib/theme-chalk/index.css';
    6. import store from './store'
    7. Vue.use(Directives)
    8. Vue.config.productionTip = false;
    9. Vue.use(ElementUI);
    10. const vm = new Vue({
    11. render: h => h(App),
    12. store,
    13. beforeCreate(){
    14. Vue.prototype.$bus = this;
    15. }
    16. }).$mount('#app')

    兄弟组件传值:

    1. //发送值:
    2. sendData(){
    3. this.$bus.$emit('send', '我是兄弟组件传过来得值')
    4. },
    5. //接收值
    6. mounted(){
    7. this.$bus.$on('send',(val) => {
    8. console.log(val)
    9. })
    10. },
    11. beforeDestroy(){
    12. this.$bus.$off('send') //销毁事件
    13. }