Skip to content

andantonyan/ngx-application-event

Repository files navigation

General Application Event Listener

This project was generated with Angular CLI version 13.3.2.

Usage

// define custom event types

class UserDeleted {}
// publish event

@Component({...})
export class AppPublisher {
  constructor(private applicationEventService: NgxApplicationEventService) {
  }

  publish(): void {
    this.applicationEventService.publish(new UserDeleted())
  }
}
// listen event in Component

@Component({...})
export class AppListener {
  
  @NgxEventListener
  private on(event: UserDeleted) {
    console.log(`New event: [${event.constructor.name}]`);
  }
}
// listen event in Service

@Service({...})
export class AppListener {
  constructor(private applicationEventService: NgxApplicationEventService) {
  }

  publish(): void {
    this.applicationEventService.publish(new UserDeleted())
  }

  listen(): void {
    this.applicationEventService.listen(UserDeleted).subscribe(console.log)
  }
}

Run example application

Run ng serve for a dev server. Navigate to http://localhost:4200.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published