Singleton Service with Subjects and Observables seems not to work

Hello friends!

I tried to share data between components with a singleton service with subjects and observables but it seems not to work:

i declared it in the providers of my app.module
i injected it in my constructors
i just can view the data in the component where i set the data, when i navigate to an other component with navCtrl.setRoot(), the data is empty again.

Can someone help me out?

My service:
import {VoltoUser} from “./voltoUser”;
import {Device} from “./device”;
import {Injectable} from “@angular/core”;
import {Observable} from 'rxjs/Observable’
import {Subject} from 'rxjs/Subject’
import {Storage} from “@ionic/storage”;

@Injectable()
export class GlobalVariables {
loggedInUser: Subject = new Subject();
devices: Subject<Device[]> = new Subject<Device[]>();
selectedDevice: Subject = new Subject();
serverUrl: string = “http://localhost:47146/Smartphone/”;

constructor(private storage: Storage) {
}

public getServerUrl(): string {
    return this.serverUrl;
}

public setLoggedInUser(user: VoltoUser) {
    this.storage.ready().then(() => {
        this.storage.set('loggedInUser', JSON.stringify(user));
    });
    this.loggedInUser.next(user);
}

public listenForLoggedInUser(): Observable<VoltoUser> {
    return this.loggedInUser.asObservable();
}

}

Any chance you’re declaring it in providers of a @Component decoration somewhere?

I tried it, but it did not work either.

I didn’t ask you to try it, I asked specifically if you are doing that. If you are, stop. If that’s not the problem, please provide a minimal complete repo that people can use to replicate the problem. It’s maddeningly difficult to diagnose problems when all relevant information needed to independently reproduce them is not provided.

i found the solution:

i don’t know why, but i changed the Subject to BehaviorSubject and now it seems working.