bus.on 接收
import { computed, defineComponent, getCurrentInstance } from 'vue'
const instance = getCurrentInstance()
if(instance){
const bus = instance?.appContext.config.globalProperties.$bus
// 一个神奇的事情,bus.on自己有触发器,不需要写在方法里 用方法名()的方式 ,就可以调用=======================这也是小重点====================
//=========只要bus.emit发布了,bus.on就能接收到;所以bus.on可以不用写在方法里,然后用方法名()的方式调用;直接就可以执行====================
bus.on('activeMenuPath', (data) => {
for(let i = 0;i < navData.value.length ; i++){
if(data == navData.value[i].path){
selectPath.value = data;
break
}else{
selectPath.value = ''
}
}
})
}
bus.emit 发布
import { computed, defineComponent, getCurrentInstance } from 'vue'
const instance = getCurrentInstance()
const activeMenu = computed(() => {
if(instance){
const bus = instance.appContext.config.globalProperties.$bus
bus.emit('activeMenuPath',route.path)
}
if(route.meta.activeMenu) return route.meta.activeMenu
return route.path
})