This is simple Event Bus with the vue 3 feature of provide, inject, Symbol.
There are different way to set up.
import { createApp } from 'vue'
import eventBusPlugin from "./eventBusPlugin.mjs"
import App from './components/App.vue'
const app = createApp(App);
app.use(eventBusPlugin);
app.mount('#app');
import { useEmit } from "./eventBusPlugin.mjs"
const emit = useEmit();
function clickTest(){
//EventBus.emit('test')
emit('test')
}
import { inject } from "vue"
import { EmitInjectKey } from "./eventBusPlugin.mjs"
const emit = inject(EmitInjectKey);
function clickTest(){
//EventBus.emit('test')
emit('test')
}
Not possible to short the useEmit.
There are two and more ways to add and remove listen.
import { inject } from "vue";
import {OnInjectKey} from "./eventBusPlugin.mjs";
const on = inject(OnInjectKey);
on('test',()=>{
console.log("TEST ROOT !s!!")
})
import {useEmitOn} from "./event/eventBusPlugin.mjs";
const $on = useEmitOn();
$on('test',()=>{
console.log("TEST ROOT !!!")
})
import {on} from "./event/eventBusPlugin.mjs";
on('test',()=>{
console.log("TEST ROOT !s!!")
})