Problem with ionic tabs in ionic version 4

I am trying to implement the tabs in ionic 4, I am using below code :

<ion-tabs color="primary" class="activity-tabs" tabbarPlacement="top">
    <ion-tab padding-horizotal class="ionTabsPublic" label="tab1" href="/Tabs/Tabs1/(tab1:tab1)">
      <ion-router-outlet stack name="tab1"></ion-router-outlet>
    </ion-tab>
    <ion-tab padding-horizotal label="tab2" href="Tabs/Tabs2/(tab2:tab2)">
      <ion-router-outlet stack name="tab2"></ion-router-outlet>
    </ion-tab>
  </ion-tabs>

Created Module called Tabs under Tabs Module Created other two components one Tab1 and Tab2

So below code is used for routing in Tabs Module :

  const routes: Routes = [
  {
    path: 'Tabs',
    component: ActivityFeedsPage,
    children: [
      {
        path: 'tab1',
        outlet: 'tab1',
        component: Tabs1Page
      },
      {
        path: 'tab2',
        outlet: 'tab2',
        component: Tab2Page
      }
    ]
  },
  {
    path: '',
    redirectTo: '/Feeds/Tabs/(tab1:tab1)'
  }
];

Now everything is working fine, but the problem is every time click tab I am making rest api call, I wrote call in

  ngOnInit() {
    
restapi call();
   
  }

But It is not calling clicking on tabs, it is calling first time only…

Could you please help me anybody how can we achieve this concept.

Thanks in advance !!!

1 Like

The problem is that the ngOnInit is only called once because the component remains initialized after navigating to it.

One thing I’ve tried doing is binding to the click event, but the tab element actually contains everything but the tab button, so that won’t work.

<!-- binding to click event doesn't work like this -->
  <ion-tab label="Contact" icon="contacts" href="/tabs/(contact:contact)" (click)="tab2Clicked($event)">
    <ion-router-outlet name="contact"></ion-router-outlet>
  </ion-tab>

The next best thing to do is to listen for router events on a high level component, such as the HomePage, and either save the data in data store like an object in a Service. Or you could use a Subject to emit the value and subscribe in other parts of your code.

Here is an example I made from the Ionic 4 tabs template:

MyService

import { Injectable } from '@angular/core';

import { Subject } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class MyService {
  
  myVar: Subject<string> = new Subject<string>();

  constructor() { }
}

HomePage

import { Component, OnInit } from '@angular/core';
import { Router, NavigationStart } from '@angular/router';

import { MyService } from '../my.service';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss']
})
export class HomePage implements OnInit {

  constructor(private router: Router, private myService: MyService) {}

  ngOnInit() {
    this.router.events.subscribe(event => {
      if(event instanceof NavigationStart) {
        if(event.url == '/tabs/(contact:contact)') {
          // get data from API
          this.myService.myVar.next('new Data');
        }
      }
    });
  }
}

ContactPage

import { Component } from '@angular/core';
import { MyService } from '../my.service';

@Component({
  selector: 'app-contact',
  templateUrl: 'contact.page.html',
  styleUrls: ['contact.page.scss']
})
export class ContactPage {

  myData = 'empty';

  constructor(public myService: MyService) {
    myService.myVar.subscribe(data => {
      this.myData = data;
    })
  }
}

1 Like

Here is the question when click on tab service call is not happens, because ngOnit() will call only once even constructor also called only once.

when click on tab, refresh should happens and get data from DB

Tabs might not have a click event but they do have an ionChange event.

so just try binding the api call to the ionChange of the tab.

depending on your preference you could also use ionWillChange or ionDidChange

<ion-tabs color="primary" class="activity-tabs" tabbarPlacement="top">
    <ion-tab padding-horizotal class="ionTabsPublic" label="tab1" href="/Tabs/Tabs1/(tab1:tab1)" (ionChange)="apiCall()">
      <ion-router-outlet stack name="tab1"></ion-router-outlet>
    </ion-tab>
    <ion-tab padding-horizotal label="tab2" href="Tabs/Tabs2/(tab2:tab2)" (ionChange)="apiCall()">
      <ion-router-outlet stack name="tab2"></ion-router-outlet>
    </ion-tab>
  </ion-tabs>

actually I was tried ionWillChange or ionDidChange but both calling only once like ngOnInit()

how about ionSelect?

But ionSelect is tab based event, if you click on tab it will call but tabs in parent component.

yes so just add ionSelect to every tab and let each call the same function.

or update your ionic/angular to the latest beta version npm i --save @ionic/angular@4.0.0-beta.11 and try if ionChange works on tabs

Yes we can do that, but I have tabs module have tabs but how to update the data inside the other component.

let say example

ion-tab label="Contact" icon="contacts" href="/tabs/(contact:contact)" (ionSelect )="tab2Clicked($event)" ><ion-router-outlet name="contact"</ion-router-outlet></ion-tab>

how to pass the data to contact component to update UI

Try like this

ionViewDidEnter {

restapi call();

}