Routing Angular Applications: Lesson 14 of 23

Child Routes

Up Next

Lazy Loading

Autoplaying in 7 seconds!

Cancel

T

Table of Contents

    he Angular 2 router allows us to create child routes that will be nested underneath a certain route.

    In our case, we'll be nesting the :username route in our about.routing.ts.

    import { ModuleWithProviders } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';
    import { AboutComponent } from './about.component';
    import { AboutUserComponent } from './about-user.component';
    import { AboutSectionComponent } from './about-section.component';
    
    const aboutRoutes: Routes = [
      {
        path: 'about',
        component: AboutSectionComponent,
        children: [
          {
            path: '',
            component: AboutComponent
          },
          {
            path: ':username',
            component: AboutUserComponent
          }
        ]
      }
    ];
    
    export const aboutRouting: ModuleWithProviders = RouterModule.forChild(aboutRoutes);

    Chris Sevilleja

    176 posts

    Founder of Scotch.io. Google Developer Expert in Web Technologies. Slapping the keyboard until something good happens.