Ionic 4 / Angular 6 : Nested child routes in tabs

Brieuc picture Brieuc · Sep 30, 2018 · Viewed 24.5k times · Source

I generated an app with the Ionic Tabs Component.

Tabs are Deliveries, About and Contact.

Then I generated a page Details.

I would like the page Details to be a child of Deliveries tab.

Meaning when I'm on a details page, I'm navigating through the deliveries tab.

Someone asked a similar question on Ionic Forum

The directory structure generated :

- about
- contact
- deliveries
    deliveries.module.ts
- details
    details.module.ts
- tabs
    tabs.module.ts
    tabs.page.html
    tabs.router.module.ts
app.module.ts
app-routing.module.ts

This is the generated app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  { path: '', loadChildren: './tabs/tabs.module#TabsPageModule' },
];
@NgModule({
  imports: [
    RouterModule.forRoot(routes)
  ],
  exports: [RouterModule]
})
export class AppRoutingModule {}

This is the generated tabs.router.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { TabsPage } from './tabs.page';
import { DeliveriesPage } from '../deliveries/deliveries.page';
import { AboutPage } from '../about/about.page';
import { ContactPage } from '../contact/contact.page';

const routes: Routes = [
  {
    path: 'tabs',
    component: TabsPage,
    children: [
      {
        path: '',
        redirectTo: '/tabs/(deliveries:deliveries)',
        pathMatch: 'full',
      },
      {
        path: 'deliveries',
        outlet: 'deliveries',
        component: DeliveriesPage
      },
      {
        path: 'about',
        outlet: 'about',
        component: AboutPage
      },
      {
        path: 'contact',
        outlet: 'contact',
        component: ContactPage
      }
    ]
  },
  {
    path: '',
    redirectTo: '/tabs/(deliveries:deliveries)',
    pathMatch: 'full'
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class TabsPageRoutingModule {}

This is a generated tabs.page.html

<ion-tabs>
  <ion-tab label="Deliveries" icon="bicycle" href="/tabs/(deliveries:deliveries)">
    <ion-router-outlet name="deliveries"></ion-router-outlet>
  </ion-tab>
  <ion-tab label="About" icon="information-circle" href="/tabs/(about:about)">
    <ion-router-outlet name="about"></ion-router-outlet>
  </ion-tab>
  <ion-tab label="Contact" icon="contacts" href="/tabs/(contact:contact)">
    <ion-router-outlet name="contact"></ion-router-outlet>
  </ion-tab>
</ion-tabs>

This is a generated deliveries.module.ts

import { IonicModule } from '@ionic/angular';
import { RouterModule } from '@angular/router';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

import { DeliveriesPage } from './deliveries.page';

@NgModule({
  imports: [
    IonicModule,
    CommonModule,
    FormsModule,
    RouterModule.forChild([
      {
        path: '',
        component: DeliveriesPage
      }
    ])
  ],
  declarations: [
    DeliveriesPage
  ]
})
export class DeliveriesPageModule {}

This is details.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { Routes, RouterModule } from '@angular/router';

import { IonicModule } from '@ionic/angular';

import { DetailsPage } from './details.page';

const routes: Routes = [
  {
    path: '',
    component: DetailsPage,
  }
];

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    RouterModule.forChild(routes)
  ],
  declarations: [DetailsPage]
})
export class DetailsPageModule {}

So, all my attempts failed, I tried to follow the same logic as above.

I tried to work in deliveries.module.ts

RouterModule.forChild([
  {
    path: '',
    component: DeliveriesPage,
  },
  {
    path: 'details',
    loadChildren: '../details/details.module#DetailsPageModule'
  }
]),

I also tried

RouterModule.forChild([
  {
    path: '',
    component: DeliveriesPage,
  },
  {
    path: 'details',
    outlet: 'deliveries
    loadChildren: '../details/details.module#DetailsPageModule'
  }
]),

Or

RouterModule.forChild([
  {
    path: '',
    component: DeliveriesPage,
    children: [
      {
        path: 'details',
        loadChildren: '../details/details.module#DetailsPageModule'
      }
    ]
  },
]),

Can't find a way to access details page from

  • /tabs/(deliveries:deliveries)/details
  • /tabs/(deliveries:details)

Is it possible to achieve that? It is a little bit confusing.

The easy way right now to access details page is to define the route in app-routing.module.ts, but it would not be part of the same router outlet.

Answer

servrox picture servrox · Feb 17, 2019

With 4.0.0-beta.18 ion-tab was removed and it's not necessary to use named outlets.

Demo (with two different approaches) + Explanation:
https://github.com/servrox/demo-ionic-tab-routing

Ionic CLI version 4.10.3
Ionic Framework @ionic/angular 4.0.1