Refreshing the side menu in Ionic 3 post-login

When it comes to displaying side menu items based on the user's role, I run into an issue in app.html. Even though I check if the page's role matches the logged-in role, the menu items don't show up right after logging in. However, upon refreshing the app using the browser refresh button, the correct menu items appear as expected. This makes me think that Ionic might be caching the menu view. If that's the case, how can I refresh the app to display new menu items?

Each time I log in with a different user/role, the menu continues to display according to the previous user/role.

app.component.ts

constructor() {
    this.initializeApp();
    this.pages = [
                    { title: 'Home', component: 'HomePage', icon: 'home', role: 5 },
                    { title: 'Profile', component: 'ProfilePage', icon: 'person', role: 5 },
                    { title: 'Account', component: 'AccountPage', icon: 'home', role: 5 },
                    { title: 'Search Employee', component: 'AtlasemployeehomePage', icon: 'home', role: 4 }
                 ];

  }

app.html

<ion-list>
      <div *ngFor="let p of pages">
        <button menuClose ion-item *ngIf="p.role == role"  (click)="openPage(p)">
          <ion-icon  name="{{p.icon}}" style="margin-right:10%"></ion-icon> {{p.title}}
        </button>
      </div>

      <button menuClose ion-item (click)="presentLogout()">
        <ion-icon name="log-out" style="margin-right:10%"></ion-icon> Logout
      </button> 
    </ion-list>

I adjust the role variable based on the currently logged-in user.

Answer №1

If you want to implement event handling in your Ionic app, you can make use of the Events API

The Events API allows you to create a publish-subscribe style event system that enables communication between different parts of your application.

import { Events } from 'ionic-angular';

// Example usage in login.ts page
constructor(public events: Events) {}
createUser(user) {
  console.log('User created!')
  this.events.publish('user:created', user, Date.now());
}

// Example usage in app.component.ts page
constructor(public events: Events) {
  events.subscribe('user:created', (user, time) => {
    console.log('Welcome', user, 'at', time);
  });
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error in AngularJS and TypeScript: Property 'module' is undefined and cannot be read

I'm attempting to incorporate TypeScript into my AngularJS 1.x application. Currently, my application utilizes Webpack as a module loader. I configured it to handle the TypeScript compilation by renaming all *.js source files to *.ts, and managed to ...

Angular displays [object Object] upon return

I have been struggling to send a post request, but unfortunately the API is returning undefined. When I try to send it to my API, it shows up as [object Object] getAccouting():Observable<any>{ // let json = this.http.get<any>('/assets/ ...

Instructions on retrieving keyboard input values from Angular's Material Datepicker using Reactive Forms

Currently, I am using Angular along with material datepicker within Reactive Forms and moment's MomentDateModule. My concern lies in extracting the value that a user types into the form via keyboard input. If you wish to see an example of this scenar ...

Unable to make a POST request with multipart/form-data using Angular

I've been attempting to send formData using Angular code, and I've implemented an httpInterceptor to add request headers. However, I'm encountering errors when trying to post the request. Below is the code snippet I'm using: httpInter ...

No tests were found to run when Karma was executed using the ng test command

I'm facing an issue with my Angular 10 project where Karma is not detecting my default and custom spec.ts files for execution. Any ideas on why this could be happening? Here is a snapshot of my unchanged Karma Config file: // Karma configuration file ...

Jest test encountering an issue where FileReader, File, and TextDecoder are not defined

While I have primarily used Jasmine for tests in the past, I am now experimenting with Jest. However, I have encountered an issue where classes like FileReader, File, and TextDecoder are not defined in my tests. How can I incorporate these classes with t ...

Loading data cards in Angular2 based on dynamic values from the database

I currently have a sample card designed using material design elements. How can I programmatically display multiple data cards based on the value stored in my database? For example, if the value retrieved from my mongoDB database is 4, then I want to dyna ...

Exploring the powers of three.js within an Angular2 environment

I'm currently working on incorporating a three.js scene into an Angular 2 page. As a beginner in both Angular 2 and JavaScript, I've added the three.js file in the script tag within index.html, like this: . Additionally, in the scene.component.ts ...

Can you guide me on how to generate a personalized stamp within the Angular component using Grapecity GcPdfViewer?

When a Pdf is opened in the GcPdfViewer by the User, they are required to stamp the Pdf with an image located in the src/assets folder. However, the example provided on this webpage is not functioning correctly. Can anyone offer me a functioning example? ...

The custom validator in Material2 Datepicker successfully returns a date object instead of a string

Im currently working on developing a unique custom validator for the datepicker feature within a reactive form group. Within my code file, specifically the .ts file: form: FormGroup; constructor( private fb: FormBuilder, ...

Learn how to retrieve data from a parent component in Angular 8 by leveraging the window.opener property in a child window

On my Angular application, I have 2 separate pages - page1 and page2. Page1 has a button that, when clicked, opens page2. Since they are different components, I need to use window.opener to access data from page1 in page2 by calling window.opener.page1Func ...

Insight on the process of submitting interactive forms

I'm facing a challenge that I can't seem to figure out It's a form structured in HTML like this: <button (click)="addform()">add form</button> <div class="conten-form"> <div class="MyForm" ...

Creating an enum in TypeScript can be accomplished by using the enum

What transformations do enums undergo during runtime in the TypeScript environment? Fruit.ts enum Fruit {APPLE, ORANGE}; main.ts let basket = [Fruit.APPLE, Fruit.ORANGE]; console.log(basket); The resulting main.js file remains identical to the .ts ver ...

Webpack 5 is failing to bundle re-exports correctly

Whilst bundling my web application, I've come across an issue with re-exports of certain modules not behaving as expected. Despite trying various optimization settings, I have been unsuccessful in resolving this issue. Setup Here's the setup tha ...

Refresh the state by selecting an object from the dropdown menu

I have a TaskTypes object with various properties nested inside. -LJLQR7lVkbzsxAZC3JI: {code: "Pest & Disease", colour: "0xf2473f", deleted: null, subTypes: {…}} -LJLQR7mZaeBNnEhdHtU: {code: "Health & Safety", colour: "0x8c5de4", deleted: null, ...

"Exploring the process of adding external JavaScript or CSS files into Angular 2 using webpack

Embarking on an angular 2 project with webpack has been quite the adventure. Utilizing the angular2-webpack-starter from https://github.com/AngularClass/angular2-webpack-starter. The task at hand is to integrate external javascripts and css into my app. D ...

Properly write a function in Typescript that returns the initial property of an object

Looking for a solution to adjust the function below to match the property type: const firstProp = (object: Record<string, unknown>) => object[Object.keys(object)[0]]; Any thoughts on how to modify the function so its return type aligns with the ...

How can I show or hide a survey pop-up depending on the URL in the address bar?

There are 2 different applications in play here. My application is built using Angular for the front end and can be accessed at http://test.com/search. It includes a JavaScript function that triggers a survey popup whenever a user visits our website. Anot ...

What is the best way to display large typescript types within VSCode?

My question pertains to a type that has a large structure, similar to the one below: type Large = { foo: 123, bar: 123, baz: 123 } | { foo: 123, bar: 123, baz: 123 } | ... When I hover over it in VSCode, it gets shortened like thi ...

Extracting specific information from JSON data

I need to extract posts from a JSON dataset based on specific tags. data: any = [ {"id": 1, "title": "title 1", "description": "Lorem Ipsum.", "by": "author", "tags": [ { "tag":"facebook" }, { "tag": "twitter" } ] ...