vue全局事件总线bus
main.js
new Vue({
el:'#app',
render: h => h(App),
beforeCreate() {
Vue.prototype.$bus = this //安装全局事件总线
},
})
子组件
<button @click="sendStudentName">把学生名给School组件</button>
methods: {
sendStudentName(){
this.$bus.$emit('hello',this.name)
}
},
父组件
mounted() {
// console.log('School',this)
this.$bus.$on('hello',(data)=>{
console.log('我是School组件,收到了数据',data)
})
},
beforeDestroy() {
this.$bus.$off('hello')
},