1,先在main.js中声明:
new Vue({
router,
render: h => h(App),
data: {
eventHub: new Vue()
}
}).$mount('#app')
data内把全局空vue实例赋值给eventHub属性,在其他组件中使用
2, 在一个组件中用
this.$root.eventHub.$emit('事件名', 参数,参数)
3,在另一个组件中使用
this.$root.eventHub.$on('事件名', (参数,参数)=>{
console.log('事件名', 参数,参数 )
})
可以接受参数并监听事件
这样就可以使用
o
n
,
on,
on,emit了。
— — 本文个人技术分享,希望能帮助到大家。