Get active nav from app.component.ts

#1

Ionic3 i Have Tried the Following Things but it not works
please help me soon

//returns view controller obj 
let view = this.nav.getActive();

//prints out component name as string
console.log(view.component.name);
#2

Read the NavController docs. There’s sample code for this, if I understand your question correctly.

#3

NavController not working in app.component.ts file it ask for No Provider For NavController

#4
//returns view controller obj 
let view = this.nav.getActive();

//prints out component name as string
console.log(view.component.name);

it works only debug mode , ionic cordova run android --prod , it returns only t

#5

You defined nav with @ViewChild the way the docs direct? Because I’ve followed that code and it’s worked for me.

#6

it works only debug mode , ionic cordova run android --prod ,(After minified)
console.log(view.component.name); it returns only t

#7

Try app to get the nav controller…

App is a utility class used in Ionic to get information about various aspects of an app

1 Like
#8

Hello thanks for your reply.
i have tried the following functions from app.
getActiveNav();
console.log(this.app.getActiveNav());

Nav {_config: Config, _elementRef: ElementRef, _renderer: RendererAdapter, _componentName: undefined, parent: null, …}

using element ref i am getting current page name. After --prod mode it only displayt
i have tried all the possibilities to get the current active page name using app provider. no way to get it.

please help me.

#9

Have you tried

this.appCtrl.getRootNav()

I have used this to navigate in an project, but never in production mode.

Or perhaps you can make a provider where you save the page name when the page did load or an other event of the page.

1 Like
#10

Thank you so much Jacktoolsnet for spending time for me

i have tried the second one using provider to save page name… it works fine thank you,


// watch network for a connection if Disconnected
       this.network.onDisconnect().subscribe(() => {
           console.log('network Disconnected!');
           if(this.InternetProvider.current_page != 'NoInternetPage'){
               this.nav.push(NoInternetPage);
               this.InternetProvider.current_page = 'NoInternetPage';
               this.InternetProvider.networkConnected = false;
              
            }
      });
2 Likes
#11

May not be the best but works.

If you have vars that are global to the controller you can access those vars by name, it may identify a class.

let page = this.nav.getActive()
page.instance.nameOfYourVar

If you have a title in the HTML page that belongs to the class that you want to find, you can find that title, it may identify the page as well.

let page = this.nav.getActive()
page._nav._app._title

You can also know which page you are on with.

let page = this.nav.getActive() 
if(page.instance instanceof yourClassNameHereNotString)
#12

I’ll go a step farther and say that any time anybody feels the need to use any of the idioms in your post, it is an indication of a major design flaw.