Lets say we have two tabs ( http://ionicframework.com/docs/v2/components/#tabs ). On Tab 1 there is a button that is supposed to open Tab 2, just like it would when we hit the actual “Tab 2”-Tab. Nav.push with the root page of Tab 2 doesnt work because it opens the page in Tab 1 …
Alright guyz. This was the thing I was struggling for AGES. Let me clear up that for you.
First you need to know how to get components from ionic.
constructor(
public app: IonicApp,
) {
this.app = app;
}
ngAfterViewInit() {
// Here 'my-content' is the ID of my ion-content
this.content = this.app.getComponent('my-tab');
console.log("Yo we get the tab", this.content);
}
After you get the tab. You can all the glory functions --> Liike
this.content.select(0);
I believe that I’ve found a simpler solution in the Ionic documentation:
"
You can also switch tabs from a child component by calling select() on the parent view using the NavController instance. For example, assuming you have a TabsPage component, you could call the following from any of the child components to switch to TabsRoot3:
I tried before @ViewChild but it doesn’t work well when there is ngIf in the ionic html file. So, to achieve what you’re asking for, I used Router to navigate to the tab since NavCtrl involves backward and forward animation, so it’s weird to do that with tabs.
Here’s how:
// inject router in the constructor of the ts file where you want to navigate from tab 1 to tab 2
constructor(private router: Router)
...
// function to open tab 2
openTab() {
this.router.navigateByUrl('tabs/tab2');
}
Bouns:
in case you want Tab 2 to know that it was loaded by action from Tab 1 and you want to do some certain action in Tab 2, you can add data in the NavigationExtras parameter of Router.navigateByUrl: