Created
October 6, 2020 17:06
-
-
Save james2doyle/22d83dc4e07a1e806801ec6d5b7ba4a6 to your computer and use it in GitHub Desktop.
Nuxt plugin for the mitt event emitter. Written in TypeScript
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
import * as mitt from 'mitt'; | |
const emitter: mitt.Emitter = mitt.default(); | |
// log all events in development | |
if (process.env.NODE_ENV === 'development') { | |
emitter.on('*', console.info.bind(console)); | |
} | |
export default emitter; |
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
import { Plugin } from '@nuxt/types'; | |
// see emitter.ts | |
import emitter from 'emitter'; | |
const eventEmitter: Plugin = (context, inject) => { | |
// Inject $emitter in Vue, context and store. | |
inject('emitter', emitter); | |
// For Nuxt <= 2.12, also add 👇 | |
context.$emitter = emitter; | |
}; | |
export default eventEmitter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment