Angular / Ionic upload file whenever page start

Asked

Viewed 215 times

0

I understand you have a few pages about Angular’s Lifecycle, but I haven’t found how to call a function every time the page finishes loading. I have a key that when starting the APP it is set in Storage

this.storage.set('X-Access-Token', 'f27bf5399b81c701bd9d6158ca67bb58');

When I start a few pages it is necessary to take this Key from Storage and call the API to load the data. Calling Storage and API is OK.

export class MyChannelPage implements OnInit {
      XAccessToken: any;

     constructor(
       private httpClient: HttpClient,
       private storage: Storage,
     ){}

      ngOnInit() {
           this.GetXAccessToken();
           this.ConsoleToLog();
      }

      GetXAccessToken() {
          this.storage.get('X-Access-Token').then((token) => {
          this.XAccessToken = token;
         });
      }

      ConsoleToLog(){
        console.log(this.XAccessToken);
      }
}

Problem is that if I call the function to load the data inside ngOnInit(), it returns me Undefined for the variable Xaccesstoken.

I believe it must be some Lifecycle that I need to use instead of ngOnInit();

I created a button to call the function and when I use the button the functions work.

Know which Lifecycle I need to use so that whenever the page is loaded the data will be updated?

  • But you’ve tried some Lifecycle?

  • @Leandrade tried a little with ngOnInit(), ngAfterViewChecked() and ngAfterContentView() but I was unsuccessful.

1 answer

1

After reading the Ionic page document (https://ionicframework.com/docs/angular/lifecycle) I saw this detail about ngOnInit -> will only be triggered every time the page is created recently, but not when it goes back to the page.

Then I started using ionViewWillEnter and ionViewDidEnter

Browser other questions tagged

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