I am building an app using the tabbed template in Ionic. It has 4 buttons. .
When the user is logged out, the 4th button should take them to the login/registration page as above.
When they are logged in, it should show a logout button as shown above.
Below is my code in tabs.html
showing my tabs:
<ion-tabs>
<ion-tab [root]="tab1Root" tabTitle="Home" tabIcon="home"></ion-tab>
<ion-tab [root]="tab2Root" tabTitle="Search" tabIcon="search"></ion-tab>
<ion-tab [root]="tab3Root" tabTitle="List Providers" tabIcon="list"></ion-tab>
<ion-tab [root]="tab4Root" tabTitle="Login/Register" tabIcon="finger-print" *ngIf="loggedIn"></ion-tab>
<ion-tab tabTitle="Logout" tabIcon="exit" (click)="logout()" *ngIf="!loggedIn"></ion-tab>
</ion-tabs>
When I test out this arrangement by manually setting the value of the boolean variable loggedIn
in tabs.ts
, it works as expected.
What I want is for the tabs to switch automatically, depending on whether or not the user is logged in. When a user successfully logs in, I am setting a value loggedIn
in local storage to true. I then have the code below in my tabs.ts
which should toggle the login/logout buttons:
loggedIn: boolean = false;
ionViewWillEnter(): void {
if (localStorage.getItem('loggedIn') == 'true') {
this.loggedIn = true;
console.log("we are logged in!");
} else {
this.loggedIn = false;
console.log("we are logged out");
}
}
The problem however, is that though the value of the variable loggedIn is being set and changed correctly, the view in tabs.html
is not updating to then show the logout button, it still shows the login/register button. How can i get the tab to refresh each time it a button is clicked? I have tried adding code to the ionViewWillEnter()
function but it did not work. Any help will be greatly appreciated!
My system:
$ ionic info
Ionic:
ionic (Ionic CLI) : 4.0.3 (/usr/local/lib/node_modules/ionic)
Ionic Framework : ionic-angular 3.9.2
@ionic/app-scripts : 3.2.0
Cordova:
cordova (Cordova CLI) : 8.0.0
Cordova Platforms : android 7.0.0
System:
Android SDK Tools : 26.1.1
NodeJS : v8.10.0 (/usr/bin/node)
npm : 3.5.2
OS : Linux 4.15
Environment:
ANDROID_HOME : /home/user/Android/Sdk