public

Angular 7 Routing: Module Routes

So we’ve covered how to setup basic routing between components in Angular 7 Routing: The Basics [https://matthewdavis.io/angular-7-routing-the-basics/] but what if we have a module with routes?

Latest Post Speed kills software engineering. by Matthew Davis public

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(..).

module-routes.module.ts

mport {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

Matthew Davis

Published 4 years ago