Created
November 13, 2023 14:57
-
-
Save eserdinyo/5acf59241f6d00f07d6f7a6a359adc30 to your computer and use it in GitHub Desktop.
Built in eventbus composable for nuxt 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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