I wasn't sure if the answer by @Zircon was what you were looking for so I will share my approach for what I think you want.
So presuming you imported your service in your app.module.ts and added it to your list of providers, in your service you can set something up like:
import { BehaviorSubject } from 'rxjs/Rx';
import { Injectable } from '@angular/core';
@Injectable()
export class YourService {
public var1: BehaviorSubject<string> = new BehaviorSubject('str');
public var2: BehaviorSubject<boolean> = new BehaviorSubject(true);
public var3: BehaviorSubject<number> = new BehaviorSubject(123);
Now whenever a component changes one of these values, all other components can listen and stay updated by having something like this is your component:
export class YourComponent implements OnInit {
myData: any = this.yourService.var1.subscribe((value) => this.myData = value);
constructor(
private yourService: YourService) { }
ngOnInit() {
}
}
You can update values by:
this.yourService.var1.next('new_str');
And if you want it so that when you load particular components they automatically update your service variables, you can place the above line within your ngOnInit(){}
statement.