beforeCreate(){ Vue.prototype.$bus = this; }
import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import Directives from './directives/index'
import 'element-ui/lib/theme-chalk/index.css';
import store from './store'
Vue.use(Directives)
Vue.config.productionTip = false;
Vue.use(ElementUI);
const vm = new Vue({
render: h => h(App),
store,
beforeCreate(){
Vue.prototype.$bus = this;
}
}).$mount('#app')
兄弟组件传值:
//发送值:
sendData(){
this.$bus.$emit('send', '我是兄弟组件传过来得值')
},
//接收值
mounted(){
this.$bus.$on('send',(val) => {
console.log(val)
})
},
beforeDestroy(){
this.$bus.$off('send') //销毁事件
}