Use the ion-back-button-tabs
component instead of ion-back-button
when you got trouble navigating from a tabbed-page to a global-page and back.
For a complete demo project, an alternative workaround and a problem description look here.
npm i ion-back-button-tabs --save
import { BackButtonTabsModule } from 'ion-back-button-tabs';
@NgModule({
imports: [BackButtonTabsModule]
})
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { TabsPage } from './tabs.page';
const routes: Routes = [
{
path: 'tabs',
component: TabsPage,
children: [
{
path: 'tab1',
children: [
{
path: '',
loadChildren: '../tab1/tab1.module#Tab1PageModule'
}
]
},
{
path: 'tab2',
...
import { Component } from '@angular/core';
@Component({
selector: 'app-tabs',
templateUrl: 'tabs.page.html',
styleUrls: ['tabs.page.scss']
})
export class TabsPage {}
<ion-header>
<ion-toolbar>
<ion-buttons slot="start">
<ion-back-button-tabs
defaultHref="/tabs/tab1"
tabsPrefix="tabs"
tabsPageSelector="app-tabs">
</ion-back-button-tabs>
</ion-buttons>
<ion-title>Global Page</ion-title>
</ion-toolbar>
</ion-header>
ion-back-button-tabs
uses the ion-back-button
internally.
There are two attributes added (tabsPrefix + tabsPageSelector)
which are used in the also added directive (ionBackButtonTabs).
<ion-back-button
[defaultHref]="defaultHref"
[tabsPrefix]="tabsPrefix"
[tabsPageSelector]="tabsPageSelector"
ionBackButtonTabs>
</ion-back-button>
The ionBackButtonTabs directive looks pretty much the same as the ion-back-button
directive,
except that it takes the correct route when navigating from a global-page to a tabbed-page.
This means that it's first checked with the help of the tabsPrefix if to navigate back to a tabbed-page.
If this is the case, the last active tab-route is determined with the help of the tabsPageSelector.
For the determination, the StackController of the ion-tabs
component view, which is located in the component view of the specified tabsPageSelector, is taken.
Nevertheless, this is not a complete clean solution because currently private attributes of the current IonRouterOutlet are used. The location attribute to get the current ViewContainerRef, which is needed to find the component views. And some more to support swipe gestures.
This project is licensed under the MIT License - see the LICENSE.md file for details
- Marcel Mayer - servrox.solutions