1、npm install mitt 添加mitt
2、单独开一个eventbus.ts,引入mitt,执行mott库导出的函数获取事件总线对象并暴露;
eventbus.ts
3、在A组件中引入eventbus.ts,
发起事件:
emitter.emit('updateMenu', {to:'/logList', from:'/statisticalReportsList'});
updateMenu是自定义事件名,后面则为参数携带的参数
4、在B组件中引入eventbus.ts,
接收事件:
emitter.on('updateMenu', msg => {
console.log('该更新菜单选中项了', msg)
});
msg是接收的参数。