[Fixed] Get selected tab to switch icons Ionic 5


I’m trying to change my tab icons from filled to outline when someone selects it (filled when selected, outline when not selected).

On the Ionic 5 Tabs Doc there’s a getSelected() method but no examples on how to use this.

My idea was to use ionTabsDidChange to detect when someone clicked a tab, then use getSelected() and set the icon from ‘home’ to ‘home-outline’.


  <ion-tab-bar slot="bottom">
    <ion-tab-button class="tab-btn" tab="home">
      <ion-icon name="{{ homeIcon }}"></ion-icon>

    <ion-tab-button class="tab-btn" tab="price">
      <ion-icon name="{{ priceIcon }}"></ion-icon>
      <ion-label>Price Search</ion-label>


export class TabsPage {
  public homeIcon = 'home';
  private homeFilled = 'home';
  private homeOutline = 'home-outline'
  public priceIcon = 'search-outline';
  private priceFilled = 'search';
  private priceOutline = 'search-outline';

  ionTabsDidChange() {
    let selectedTabName = getSelected();
    // Stuff to switch icon from filled to outline and vice versa


The issue is that I don’t know how to use getSelected(), I’ve tried adding ViewChild like this stackoverflow, but getSelected() is not a function (Changed to IonTabs because Tabs don’t exist in Ionic 5.

At this point, the only solution I can think of is saving the tabs state and adding click functions for everything.


You are heading the right direction, there are still few missing points. In the Ionic doc you are reading the "events" are not directly accessible in the page without binding them to the component itself and in order to use ViewChild you also need to give the component an id:


<ion-tabs #tabs (ionTabsDidChange)="setCurrentTab()">

"tabs" will be the id of the component and whenever ionTabsDidChange event gets triggered it will call setCurrentTab method, it should be declared on your page.

Then in the page, as you have already mentioned you’ll need to add a ViewChild (now possible with the id) and use getSelected() method.


export class TabsPage {
  @ViewChild('tabs', { static: false }) tabs: IonTabs;


  setCurrentTab() {
    this.selectedTab = this.tabs.getSelected();


And voila, that should be it 🙂

Leave a Reply

(*) Required, Your email will not be published