Search code examples
angulartypescripteventseventaggregator

Angular 2: Is there an eventaggregator?


I need a central, singleton Angular 2 event aggregator for cross component/module communication.

Is there an event aggregator for Angular 2 (7.3.6), that is "ready-to-go" and which can be installed with npm?

I have seen few custom implementations, but I could not find any "official" or "widely accepted" node module for this purpose.


Solution

  • There two types of implementations that I know of and have used in projects. Depending on your requirements, both may fit your request:

    NGX Message Bus

    A basic "bus" style lib, enables exchanging messages / events between components or among the whole of your application.

    https://www.npmjs.com/package/ngx-message-bus

    Redux based communication / state management

    Depending on the scale of your application (read: if it's rather big), you may be interested in Redux-style libraries like NGRX, NGXS or Akita. Strictly speaking, these libs are not purely messaging systems, but state management projects. Redux has a lot of advantages (immutable state, traceable actions and state changes) but usually also comes with a learning curve.