Codementor Events

Angular 7 Routing: Module Routes

Published Nov 01, 2018
Angular 7 Routing: Module Routes

So we’ve covered how to setup basic routing between components in Angular 7 Routing: The Basics but what if we have a module with routes? Routing is handled pretty much the same except for one piece. We’ll be using the RouterModule.forChild() method to inject our routes instead of RouterModule.forRoot(..).

https://vimeo.com/298319739

module-routes.module.ts

import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {ComponentAComponent} from './component-a/component-a.component';
import {ComponentBComponent} from './component-b/component-b.component';
import {RouterModule} from '@angular/router';

@NgModule({
    imports: [
        
        CommonModule,

        RouterModule.forChild([

            {

                path: 'component-a',
                component: ComponentAComponent

            }, {

                path: 'component-b',
                component: ComponentBComponent

            }
        ])
    ],
    declarations: [ComponentAComponent, ComponentBComponent]
})
export class ModuleRoutesModule {
}

Source Code

See more at https://matthewdavis.io

Discover and read more posts from Matthew Davis
get started