Update app variable-Angular Component

Asked

Viewed 143 times

0

I have a menu in the app-Component and need to update the Islogged variable after user login so that the menu appears correct after login successfully.

<ion-item routerLink="home" *ngIf="!IsLogged">
    <ion-icon slot="start" name="log-in-outline"></ion-icon>
    <ion-label>{{'sidemenu.signin' | transloco}}</ion-label>
</ion-item>
            
 <ion-item  (click)="DoLogOut()" *ngIf="IsLogged"> 
      <ion-icon slot="start" name="log-out-outline"></ion-icon>
      <ion-label>{{'sidemenu.signout' | transloco}}</ion-label>
 </ion-item>

 <ion-item  routerLink="dashboard" *ngIf="IsLogged">
      <ion-icon slot="start" name="apps-outline"></ion-icon>
      <ion-label>{{'sidemenu.app' | transloco}}</ion-label>
 </ion-item>

I created an observable where returns the Islogged value as true, but after login the variable in the app-Component is not automatically updated, some method to make this update, for example the Component stay "listening" changes in the variable?

this._LoginService.Session().subscribe(data => { this.IsLogged = data.IsLOgged }).unsubscribe;
  • Just give me one console.log(date) to see what is coming, if nothing is coming there the problem is in this service!

  • It is arriving yes, this observable inclusive use throughout the APP.. but the menu does not update, since it loads before the user logs in.

1 answer

0

I found a site explaining how to hide / view menu based on user login.

http://loiane.com/2017/08/angular-hide-navbar-login-page/

observable is being implemented differently from what it was using, I made another observable like the one shown on the site and it worked

private IsLoggedIn = new BehaviorSubject<boolean>(false);

get _IsLoggedIn() {
    return this.IsLoggedIn.asObservable(); 
}

after successfully executed login, returns.

this.IsLoggedIn.next(true);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.