I would probably use a service that uses Subject . Thus, it can be published and subscribed to
import { Subject } from 'rxjs/Subject'; class SomeService { private _subject = new Subject<any>(); newEvent(event) { this._subject.next(event); } get events$ () { return this._subject.asObservable(); } }
From your components you can publish and subscribe
@NgModul({ providers: [ SomeService ] }) class AppModule {} @Component() class ComponentOne { constructor(private service: SomeService) {} onClick() { service.newEvent('clicked!'); } } @Component() class ComponentTwo { constructor(private service: SomeService) {} ngOnInit() { this.service.events$.forEach(event => console.log(event)); } }
See also:
source share