angular - subscribing to BeahviorSubject in app.component constructor only triggers on first app load -
using angular2 in ionic 3 project:
i need change menu in app.component based on user subscription plan. in order after login, have currentuser$ asobservable of behaviorsubject. code setup:
app.component.ts -> constructor:
this._authprovider.currentuser$ .subscribe(currentuser => { //this._accountsandsettings = accountsandsettings triggered++; console.log('triggered', triggered); if (currentuser.license === "agency") { this._isagency = true; }; ._menucontroller .enable(true); this._rootpage = "maintabspage"; }, accountsandsettingserror => { //display alertcontroller , call platform.ready() this._rootpage = "authloginpage"; });
loginpage.ts - >login:
private _login(): void { //check missing credentials if (this._loginform.invalid) { //alert pop-up let missingcredentialsalert = ._alertcontroller .create({ message: "some credentials missing", buttons: [ { text: "ok", role: 'cancel' } ] }); missingcredentialsalert.present(); //call login } else { let loginloading = ._loadingcontroller .create({ content: "logging in ..." }); loginloading.present(); let loginrequestbody = { email: this._loginform.value.email, password: this._loginform.value.password } .authprovider .login(loginrequestbody) .subscribe(currentuser => { loginloading.dismiss(); }, currentuser => { loginloading.dismiss(); let alert = ._alertcontroller .create({ title: null, message: currentuser.msg_id, buttons: [ { text: "ok" } ] }); alert.present(); }); };
authprovider -> constructor:
private _currentusersource: behaviorsubject<userinfo> = new behaviorsubject(null); public updatedcurrentuser$ = ._currentusersource .asobservable();
authprovider -> login:
public login(loginrequestbody): observable<object> { return ._http .post('/user/login', loginrequestbody) .flatmap(response => { return observable.frompromise(this._ionicstorage.set(this._keyforuserinfoinstorage, response.json().userandsettings).then(saveduserinfo => { ._currentusersource .next(saveduserinfo); return saveduserinfo; })); }) .catch(loginerror => { return observable.throw(loginerror); }); }; end login
after logging in agency user, expect page change maintabspage agency menu (_isagency used in *ngif show additional menu items). happening after login page remains on login, , subscribe not triggered in app.component constructor.
Comments
Post a Comment