Skip to content

Instantly share code, notes, and snippets.

@eserdinyo
Created November 13, 2023 14:57
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eserdinyo/5acf59241f6d00f07d6f7a6a359adc30 to your computer and use it in GitHub Desktop.
Save eserdinyo/5acf59241f6d00f07d6f7a6a359adc30 to your computer and use it in GitHub Desktop.
Built in eventbus composable for nuxt 3
// composables/useEmitter.ts
export default function () {
const hooks = useNuxtApp().hooks;
return {
emit: hooks.callHook,
on: hooks.hook,
}
}
// usage in components
someFunction(){
const emitter = useEmitter();
emitter.emit("test-emit", "hello world");
}
// anoter components
const emitter = useEmitter();
emitter.on("test-emit", (payload) => {
console.log(payload);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment