Update:
Actually, I have implemented "toggle menu" as on this doc.Please see that.The toggle menu is working fine.But I need to get the latest token when a user clicks the toggle menu.It works only with the constructor.After that, I cannot do that.That is may be due to the Root component.How can I achieve that?
Which life cycle event can I access after creating the app.component.ts
component? First time it fires constructor()
code.But after that none of the lifecycle events are working.
My scenario: I have implemented side menu as shown below on Ionic2 app.
app.html
<ion-menu [content]="content">
<ion-content padding>
<img src="./assets/img/login.png" alt="Login" (click)="login()" *ngIf="token!=null && token.length==0" menuClose>
//removed for clarity
</ion-content>
</ion-menu>
<ion-nav [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>
Now I need to get the token value each and every time user clicks on above menu.But it works only once.After that, it's not working.Can you tell me a workaround for this?
app.component.ts
export class MyApp extends HandleStorageService {
@ViewChild(Nav) nav: Nav;
rootPage = EventSchedulePage;
menuList: Observable<any>;
token: string = '';
constructor(platform: Platform, public menuData: MenuData, public loadingCtrl: LoadingController, public storage: Storage) {
super(storage);
platform.ready().then(() => {
StatusBar.styleDefault();
Splashscreen.hide();
});
this.getMenuList();//menu list
}
ionViewDidEnter() {// not working this
this.getToken().then((val) => {//get token
this.token = val;
});
}
I will use the flag
isLoggedIn
thantoken
. So, yourapp.html
will become like this:Now in your
app.ts
, first you subscribe to these events. And you can publish these events from anywhere in the app. These subscriptions inapp.ts
gets triggered as soon as the particular event is published.app.ts
code: (Adding just the changes but not an entire code)Now say you have another component from where you are logging in. There when the login is done you can publish
user:login
event like this:Same goes for
user:logout
. Refer this for more clarity on using Events.