I got the same issue, and just found a workaround from Ionic team member liamdebeasi:
https://github.com/ionic-team/ionic-framework/issues/16834
He explains very well about this situation and provides a workaround. I just copy the code here, for more details please visit the link.
Hi everyone,
I wanted to provide an update regarding the status of this issue.
After discussing with the team, we have determined this is not a bug
in Ionic Framework; however, we realize there are valid use cases in
which developers may want to listen for lifecycle events on an
individual tab. Due to this, we have provided a temporary workaround
as well as plans for further development.
Why is this not a bug?
When pages transition in Ionic Framework, they fire lifecycle events.
For example, going from /page1 to /page2 would fire ionViewWillLeave
and ionViewDidLeave events on the Page1 component and ionViewWillEnter
and ionViewDidEnter events on the Page2 component. The same logic
applies when going from /tabs/tab1 to /tabs/tab2. In both of these
scenarios, we are staying within the same parent ion-router-outlet
context.
The reported issue occurs when navigating between ion-router-outlet
contexts. In this case, we are seeing it when navigating from
/tabs/tab1 to /page2. When this transition happens, Tab1 remains the
active tab, and the entire tabs context transitions away. As a result,
lifecycle events will fire on the root TabsPage component, but not on
Tab1.
For many users, this is unexpected because Tab1 visually appears to
transition away. However, under the hood, the entire tabs context
transitions away.
What is the workaround?
Luckily, there is an easy to use workaround for developers who wish to
listen for lifecycle events on individual tab pages:
tabs.page.html
<ion-tabs #tabs (ionTabsDidChange)="tabChange(tabs)">
...
</ion-tabs>
tabs.page.ts
import { Component } from '@angular/core';
import { IonTabs } from '@ionic/angular'
@Component({
selector: 'app-tabs',
templateUrl: 'tabs.page.html',
styleUrls: ['tabs.page.scss']
})
export class TabsPage {
private activeTab?: HTMLElement;
constructor() {}
tabChange(tabsRef: IonTabs) {
this.activeTab = tabsRef.outlet.activatedView.element;
}
ionViewWillLeave() {
this.propagateToActiveTab('ionViewWillLeave');
}
ionViewDidLeave() {
this.propagateToActiveTab('ionViewDidLeave');
}
ionViewWillEnter() {
this.propagateToActiveTab('ionViewWillEnter');
}
ionViewDidEnter() {
this.propagateToActiveTab('ionViewDidEnter');
}
private propagateToActiveTab(eventName: string) {
if (this.activeTab) {
this.activeTab.dispatchEvent(new CustomEvent(eventName));
}
}
}