How to get current active Component in RC0?


#1

Till beta , this use to work for me

if(view.componentType.name == 'HomePage')

In RC0 , due to minification name returns ‘t’

How do i get the component name ?

Thanks


#2

Why do you need the current component name Oo? In which use case?


#3

I override the back button behaviour in my app.

In which , if user presses back button on any page , user should go on first tab. If user is on first tab , user is asked to press back button twice to exit.


#4

okay then as workaround:

Get the navcontroller instance --> retrieve the first view in history stack
Check if this is the active one:

if so --> ask to exit, else goto first tab
No need to check on explicit name strings


#5

I will revert once I try it. But , overriding back button also breaks popver navigation.

Like if popover is open , and user hits back then popover won’t close instead it will navigate and popover still remains open.

For this , I had a weird hack, by checking state

     if(child['_views'][0].componentType.name == 'ContactPage' && child['_views'][0].state!=1){
                    // this assumed popover was open
                     }else{
                         t.select(0);
                       }

#6

As you said I did this ,

let firstView : any;
firstView = this.nav.first();
if(this.nav.isActive(firstView)){
     console.log('first tab active');
  }else{
      console.log('Go to first tab');
}

And the above returns 'first tab active' on every tab.


#7

I don’t understand why there is no a function to retrieve the view name.

I’m setting a custom id inside the view I want to check to compare, but we have to dig into long paths to even access the view variables.

So, if I want to access the variable ‘idView’ from the activeView from anywhere, like app.component.ts

this.nav.getActive().getNav()['_views'][0].instance.idView

Ionic 3.3.0


#8

IMHO, any design that needs this functionality is broken.

Pages should be self-contained. If something has to change (like back button behavior) when a page is active, add and remove listeners using the page’s lifecycle events. Don’t centralize all this in a massive nerve center. The code becomes unreadable and untestable because you have action-at-a-distance all over the app.


#9

Found a way to compare the active view without using name!

Example: We want to know, from anywhere, if the active view is the page ‘gallery’.

import { GalleryPage } from '../gallery/gallery';

if (this.nav.getActive().component == GalleryPage){ dosomething}