问题
I am using Ionic 3 and want to get the name of the active page in order to identify which page I am on in an event subscription.
When I am on a page, the following works:
this.nav.getActive().name
However, if my root is tabs and I want to identify the page loaded in a tab, the above code just returns the tab page name and not the active page.
Question
How do I get the active page name or identifier if it is loaded in a tab?
回答1:
You can get the component of the page.
Like this this.nav.getActive().component
In the tabs page get your tabs element in ViewChild like this:
@ViewChild('myTabs') tabRef: Tabs;
After that attempt to get selected tab:
this.nav.getActive().component.tabRef.getSelected().root
This will give you the page
回答2:
In Ionic 3.9.2 you can get even the instance of the current Page with
this.viewController.instance
This lets you access to all the Page class/member methods with a simple cast.
<YuorPageClass>this.viewController.instance.yourMethod()
The official documentation is incomplete, or outdated at the moment.
来源:https://stackoverflow.com/questions/43809063/ionic-get-active-page