Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Created October 6, 2020 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save james2doyle/22d83dc4e07a1e806801ec6d5b7ba4a6 to your computer and use it in GitHub Desktop.
Save james2doyle/22d83dc4e07a1e806801ec6d5b7ba4a6 to your computer and use it in GitHub Desktop.
Nuxt plugin for the mitt event emitter. Written in TypeScript
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;
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