Search code examples
angularangular2-routing

Angular2: What's the equivalent of the router 'loadChildren' without lazy loading


I'm trying 'plug' a sub ngModule (a feature module ) containing routing configurations (imported from RouterModule.forChild() ) into a parent ngModule.

When using lazy loading, specifying where to 'plug' the child module is done using the loadChildren key in the parent module route configuration.

ex:

export const parentModuleRouteConfig = [{
    path: 'myfeaturemodule',
    loadChildren: '/app/child.module'
}];

Actually, i don't whant to use lazy loading.

How can i tell the router to 'plug' (or use) the route config specified in a sub module at a given path ?


Solution

  • With AOT

    Export your child routes instead of adding them to your child module.

    export const ChildRoutes: Routes = [
        { path: '', component: ChildComponent }
    ];
    

    Import the child module into the parent module and include routes directly.

    const parentModuleRouteConfig: [{
        path: 'myfeaturemodule',
        // loadChildren: () => ChildModule
        children: ChildRoutes
    }];
    
    @NgModule({
        imports: [
            ChildModule
        ]
    })
    export class ParentModule { }
    

    Without AOT

    You can still use loadChildren to load synchronously. There are some examples in this github issue.

    import { ChildModule } from '/app/child.module';
    
    export const parentModuleRouteConfig = [{
        path: 'myfeaturemodule',
        loadChildren: () => ChildModule
    }];