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();
}
}