VOOZH about

URL: https://quasar.dev/quasar-utils/event-bus-util/

⇱ EventBus Util | Quasar Framework


assignment_late
Why donate
travel_explore
API Explorer
build
Quasar CLI (with Vite)
Upgrade Guide
Creating a New Project
The /quasar.config File
Convert q/app-webpack Project
Browser Compatibility
TypeScript Support
Directory Structure
Commands List
CSS Preprocessors
Page Routing with VueRouter
Lazy Loading - Code Splitting
Handling Assets
Boot Files
Prefetch Feature
API Proxying
Handling Vite
Handling import.meta.env
State Management with Pinia
Lint and Format Code
Testing & Auditing
Developing Mobile Apps
Ajax Requests
Opening Dev Server To Public
build
Quasar CLI (with Webpack)
EventBus Util
v2.8.4+

Quasar supplies a global EventBus, especially useful when upgrading from Quasar v1 where the native Vue 2 interface has been dropped.

Methods

class EventBus {
 on(event: string, callback: Function, ctx?: any): this
 once(event: string, callback: Function, ctx?: any): this
 emit(event: string, ...args: any[]): this
 off(event: string, callback?: Function): this
}

Usage

import { EventBus } from 'quasar'

const bus = new EventBus()

bus.on('some-event', (arg1, arg2, arg3) => {
 // do some work
})

bus.emit('some-event', 'arg1 value', 'arg2 value', 'arg3 value')

When using TypeScript the events can be strongly-typed:

Quasar v2.11.11+

import { EventBus } from 'quasar'

const bus = new EventBus<{
 'some-event': (arg1: string, arg2: string, arg3: string) => void
 other: (arg: boolean) => void
}>()

bus.emit('some-event', 'arg1 value', 'arg2 value', 'arg3 value')

Convenience usage

Create a file in your app where you instantiate and export the new event bus then import and use it throughout your app.

Alternatively, when on a Quasar CLI project, for your convenience (so NOT required) you can create a boot file and supply an event bus (make sure that you register it in quasar.config file > boot):

A Quasar CLI boot file (let's say /src/boot/bus.js)

import { EventBus } from 'quasar'
import { defineBoot } from '#q-app'

export default defineBoot(({ app }) => {
 const bus = new EventBus()

 // for Options API
 app.config.globalProperties.$bus = bus

 // for Composition API
 app.provide('bus', bus)
})

Then, in any of your components, you can use:

// Options API
this.$bus

// Composition API
import { inject } from 'vue'

const bus = inject('bus') // inside setup()

Caught a mistake?Edit this page in browser
1. Introduction
1.1. Methods
1.2. Usage
1.3. Convenience usage
Copyright © 2015-present PULSARDEV SRL, Razvan Stoenescu