There is a service that will be provided in the main module. The idea is that when the this.userSettings object changes through the setSettings method, the subscribers of the observer receive a new object (a new link to the object). The signature occurs in different components by calling the method of the getSettingsObservable service.

constructor() { this.userSettingsObservable = new Observable((observer: Observer<UserSettings>) => { this.userSettingsObserver = observer; }); } getSettingsObservable() { return this.userSettingsObservable; } setSettings(path: string) { this.userSettings = _.set(this.userSettings, path, value); this.userSettingsObserver.next(_.clone(this.userSettings)); } 

Examples of the signature on Observable in components:

 ngOnInit() { this.userSettingsService.getSettingsObservable().subscribe( (userSettings) => { this.userSettings = userSettings; } ); } 

The question is that subscribe works only in one component, in the last one that subscribes. How to make everyone work?

thank

If it helps, here’s the service code: https://github.com/pakhuta/siarhei.pakhuta.angular2/blob/master/src/app/shared/user-settings.service.ts#L41

and this is one of the Observable subscription sites (this is exactly what works): https://github.com/pakhuta/siarhei.pakhuta.angular2/blob/master/src/app/weather/weather.component.ts#L43

    1 answer 1

    Exactly the same question asked in English stackoverflow: https://stackoverflow.com/questions/41491808/how-did-several-subscription-on-one-observable

    Instead of Observable, they suggested using BehaviorSubject

     this.userSettingsObservable = new BehaviorSubject(this.userSettings); this.userSettingsObservable.next(_.clone(this.userSettings)); 

    And everything is on fire)))) all subscribers are working!