Created
June 27, 2022 02:57
-
-
Save ManUtopiK/6bb04f918ce910d88eb15def2eba462a to your computer and use it in GitHub Desktop.
Events emitter/listener in Nuxt 3
This file contains hidden or 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
// Working code with Vue3 | |
// Create Folder plugin > Create file useEmitter.js | |
import { defineNuxtPlugin } from '#app' | |
import mitt from 'mitt' | |
const emitter = mitt() | |
export default defineNuxtPlugin((nuxtApp) => { | |
nuxtApp.provide('bus', { | |
$on: emitter.on, | |
$emit: emitter.emit, | |
}) | |
}) | |
// Emit the event from a page to a component. Add below code inside setup function | |
const { $bus } = useNuxtApp() | |
$bus.$emit('clickEvent', { "page": "Demo Page" }) | |
// Component where you want to receive the event. Add below code inside setup function | |
const { $bus } = useNuxtApp() | |
$bus.$on('clickEvent', (data) => { | |
// do whatever you want with data | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment