NavController or ViewController .component is not working in --prod mod


#1

Hello,
I’m trying to get the view name when using --prod mode.
In the constructor of the page i make this:
console.log("Active view is Login?: ", this.viewCtrl.component == LoginPage); and works fine. I get TRUE.

But, one line before that i make this:

document.addEventListener(‘backbutton’, ()=>{
if(this.viewCtrl.component == LoginPage){
appMinimize.minimize();
} else {
this.goBack();
}
}, false);
console.log("Active view is Login?: ", this.viewCtrl.component == LoginPage);

It gives me an error: Uncaught TypeError: Cannot read property ‘component’ of undefined.
This is when hit the Hardware back button of the android device.

Any help please?


#2

Hello,
I’m pretty sure it’s because of the asynchronous nature of Ionic / Angular.
Try to use a .then() statement (rework a bit your code) before the document.addEventListener, this should work. (or an if statement).


#3

There is almost always a better alternative to direct DOM access in an Ionic app, and this is no exception. Use registerBackButtonAction() of Platform instead.

I also think it’s a mistake to write code that tries to figure out what page is active, because it is action at a distance, which is hard to read, test, and maintain. Instead, initiate and tear down actions using the lifecycle events of the relevant page.