Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 2 : How to check current page is root page in Ionic 2?

I just want to check current page name in Ionic 2 for that I have used NavController in app.component.ts, but this giving an error that No provider for NavController. Please suggest me some solution, Thanks in advance.

This is My Code below :

constructor(platform: Platform,public alertCtrl: AlertController,public navControll:NavController) {
    this.platform=platform;
    //this.nav=nav;
    platform.ready().then(()=>{
     if(navControll.canGoBack())
     {
         platform.registerBackButtonAction(()=>this.myHandlerFunction());
     }  
      StatusBar.styleDefault();
      Splashscreen.hide();
})
}
like image 932
pradip shinde Avatar asked Dec 15 '22 00:12

pradip shinde


2 Answers

You can use the navControll.canGoBack() method.

It'll return true if there's a page to go back, so if it returns false it's in the root.

like image 133
Gabriel Barreto Avatar answered Dec 28 '22 10:12

Gabriel Barreto


In app.component you do not need to import NavController, because you have:

@ViewChild(Nav) nav: Nav;

instead you can try this:

this.nav

in other components:

import { NavController } from 'ionic-angular';
export class Page {
   constructor(
      public navController: NavController
   ) {}
}
this.navController

to see what you can do with NavController check this link. I think you must work with page index not page name.

like image 23
amin arghavani Avatar answered Dec 28 '22 09:12

amin arghavani