Set the active tab in Ionic 4


#1

I want to change the tab from certain pages in the app.

Using:

private app: App does not work in Ionic 4

I also created a local ref on the ion-tabs element and tried setting it using the ‘select()’ method - but i get this error:

TypeError: e.setActive is not a function

How can I select the tab programatically in Ionic 4
?


#2

Feel free to completely ignore my opinion, but I think that it is part of a framework’s job to steer developers towards good UX choices, and this is a situation where what you are trying to do should be hard, if not impossible.

The natal metaphor for tabs comes from filing cabinets, where one could (for example) switch amongst “customers”, “billing”, and “orders”: all completely separate and independent groups of information. Tabs allow them to share screen real estate, by having only one be active at a time.

What goes on in each tab needs to be confined to activities in that tab: if I leave tab A to go to tab B and then come back to tab A, it should be just as I left it.

The decision to switch tabs should be at the sole discretion of the user. Sometimes there are circumstances under which some tabs might not be available: disable them instead of removing them from the DOM, and never “warp” the user magically from one tab to another.

To summarize, if I found myself in your position, my first thought would be “programatically shifting UI focus is generally a disorienting user experience. Am I absolutely sure I want to do that?” And if I’m still convinced that it is, then I would look for a different way to present the UI than tabs, because users would expect tabs to work in a certain way, and I’m not going to uphold that expectation.


#3

Agreed the separation is better towards UX, however I am interested in knowing just how one would go about switching from tab to tab.