3.1.0 • Published 2 years ago

ngx-global-events v3.1.0

Weekly downloads
46
License
MIT
Repository
-
Last release
2 years ago

ngx-global-events | Angular6+

A really simple way to emit and listen global events in Angular 6+.

Installation

$ npm i ngx-global-events --save

How to use?

To import:
// component.ts
import { NgxGlobalEventsService } from 'ngx-global-events';

constructor(
    private globalEventsService: NgxGlobalEventsService
) { }
To emit:
// without data
this.globalEventsService.get("anyEventWithAName").emit();
// or 
this.globalEventsService.emit("anyEventWithAName");

// with data
const dataToEmit = "Hello world!";
this.globalEventsService.get("anyEventWithAName").emit(dataToEmit);
// or
this.globalEventsService.emit("anyEventWithAName", dataToEmit);
To listen:
// without data
this.globalEventsService.get("anyEventWithAName").subscribe(() => {
  // code to do when listen something
});
  
// with data
this.globalEventsService.get("anyEventWithAName").subscribe((data) => {
    console.log(data); // "Hello world!";
    // code to do when listen something
});
On any event:
this.globalEventsService.onEvent.subscribe((data: NgxGlobalEvent) => {
  // code to do when listen something
})

// NgxGlobalEvent
// {
  // eventName: 'name of event - is a string',
  // data: ['data emitted - is anything']
// }