Skip to content

Instantly share code, notes, and snippets.

@ManUtopiK
Created June 27, 2022 02:57
Show Gist options
  • Save ManUtopiK/6bb04f918ce910d88eb15def2eba462a to your computer and use it in GitHub Desktop.
Save ManUtopiK/6bb04f918ce910d88eb15def2eba462a to your computer and use it in GitHub Desktop.
Events emitter/listener in Nuxt 3
// 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