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')
		},
posted @   喜欢Tb  阅读(3)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)

阅读目录(Content)

此页目录为空

点击右上角即可分享
微信分享提示