ionViewCanEnter doesn't work as expected


I’m using ionic 3.20

I think ionViewCanEnter doesn’t work as expected. My assumption was that the view won’t be rendered if it returns false. What I see is that the view is rendered en displayed for a short time (0.5 second?), then I’m redirected to the login page. Preferably I like the view not being rendered if it returns false.

I implemented it like this:

  ionViewCanEnter() {
    return this.auth.authenticated(this.navCtrl);

And auth.authenticated is implemented like this:

authenticated(nav: NavController): boolean | Promise<any> {
    return"token").then(token => {
      console.log('from storage: ' + token);
      if (token == null) {
        setTimeout(() => {
        }, 0);
      } else {
        return true;
    }).catch(() => {
      setTimeout(() => {
      }, 0);
      return false;

Please let me know how I can improve this.


Ok, but is there a workaround? Or how can I improve thanks?

Or do you suggest another temporary solution ?

Is it really the purposes of the ionViewCanEnter function to actually change the navstack?

I’d say it should only return true or false, so the pushing page can use a catch to figure out what to do.

Ok maybe, but how does that solve the issue that the page gets rendered for a while and then redirects to the SignInPage

Well, not sure if that is relevant in the case of the use I am referring to as then Ionic will refuse to allow the navigation to pass and hence the page to render at all (according to spec)

Now you are actually not resolving to true or false, using storage to retrieve data and a timeout (of 0) to eventually set the nav. These steps may take a bit.