Skip to content

Instantly share code, notes, and snippets.

@ptitb
Forked from eserdinyo/useEmitter.ts
Created September 30, 2024 18:37
Show Gist options
  • Save ptitb/4da360e3f80b5f55e779cab3760ba156 to your computer and use it in GitHub Desktop.
Save ptitb/4da360e3f80b5f55e779cab3760ba156 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