Ionic 3 refresh the left menu after logging in

1

I need to show elements of the side menu according to the role of the user. then I look in app.html if the page shows the items corresponding to the role. but it does not show elements in the side menu just after logging in, but after updating the application with the browser's Refesh button it shows the correct elements as it should. I guess the problem is Ionic: How can I update the application so that new menu items appear?

Every time I log in with another user / role, it shows the menu according to the previous user / role.

app.component.ts

if (this.userData.roles["0"]["name"] == "bikeuser") {
        this.pages = [
          { title: "Mi perfil", icon: 'contact', component: ProfilePage },
          { title: "Solicitar", icon: 'bicycle', component: RequestAngelPage },
          { title: "Seguimiento", icon: 'photos', component: TrackingCardPage },
          { title: "Terminados", icon: 'close-circle', component: RequestFinishBikeuserPage }
        ];


else if (this.userData.roles["0"]["name"] == "Biciangel") {
        this.pages = [

          { title: "Mi perfil", icon: 'contact', component: ProfilePage },
          { title: "Sin asignar", icon: 'body', component: UnassignedRequestsPage },
          { title: "Asignadas", icon: 'people', component: RequestsPage },
          { title: "Terminados", icon: 'close-circle', component: RequestFinishedPage }
        ];
      }

app.html

 <button ion-item detail-none *ngFor="let page of pages" (click)="openPage(page)">
        <ion-icon *ngIf="page.icon" name="{{page.icon}}" item-left></ion-icon>
        {{page.title}}
      </button>

That is, if login does not show My profile. But when I give update to the browser if it shows the item my profile and the other items. Please an explanation according to what I have just placed because I have searched the internet and I can not find anything about it.

    
asked by santiago 22.12.2017 в 05:40
source

1 answer

0

You could use the module Events .

Example (simplified), explanation in the comments:

import { Events } from 'ionic-angular';

// Con publish asignas el valor, ej. si el usuario ha iniciado o no
constructor( public events: Events ) {}

is_logged_user( val ) {

  this.events.publish('usuario:iniciado', val );
}


// Y luego puedes comprobar el valor con subscribe
// en la página que deseas 

public is_logged: boolean;    

constructor( public events: Events ) {

  this.is_logged = false;

  events.subscribe('usuario:iniciado', val => {

      this.is_logged = val;        
  });
}

Then you can check if the user has logged in or not with the variable is_logged and show the content according to value:

<ng-container *ngIf="is_logged">
   // Muestra contenido si el usuario ha iniciado sesión
</ng-container>
    
answered by 22.12.2017 в 07:38