Vue 3 Event Bus with Composition API


I have setup mitt and trying to dispatch event to another component but I am having hard time because in the setup() method it doesn’t have this for accessing app instance.

Here is what I tried:

import App from './App.vue'
const el = document.getElementById('app')

import mitt from 'mitt';
const emitter = mitt();

const app = createApp(App)
app.config.globalProperties.emitter = emitter;

And in the component, I want to dispatch an event

export default {
   setup() {
      function toggleSidebar() {

          console.log(this); // binds to setup(), not the vue instance.

As this doesn’t exist, I can’t access the .emitter. What am I missing? How to use officially suggested mitt in Vue 3 composition api?

By the way if I use the v2 syntax, I can access this.emitter. But I am curious about Composition API way

export default {
  mounted() {
    console.log(this.emitter); // works


To use an event bus in Vue 3 Composition API, use Vue 3’s new provide api in main.js, and then inject in any component:

1. Install mitt:

npm install mitt

2. Provide:


import { createApp } from 'vue';
import App from './App.vue';

import mitt from 'mitt';                  // Import mitt
const emitter = mitt();                   // Initialize mitt

const app = createApp(App);
app.provide('emitter', emitter);          // ✅ Provide as `emitter`

3. Inject

3a. Any ComponentEmit an event

import { inject } from 'vue'

export default {
  setup() {
    const emitter = inject('emitter'); // Inject `emitter`
    const mymethod = () => {
      emitter.emit('myevent', 100);
    return {

Call mymethod from a button click or something.

3b. Any ComponentListen for the event

import { inject } from 'vue'

export default {
  setup() {
    const emitter = inject('emitter');   // Inject `emitter`

    emitter.on('myevent', (value) => {   // *Listen* for event
      console.log('myevent received!', `value: ${value}`);


myevent received! value: 100 

Answered By – Dan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published