Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 555
Clement Avatar asked Jan 18 '17 15:01

Clement


People also ask

What is LoadChildren in Angular routing?

Use LoadChildren:For lazy loading. Using this property will optimize your application's performance by only loading the nested route subtree when a user navigates to a particular URL that matches the current route path. It helps in keeping the nested routes table separate.

What is routing and lazy loading?

Lazy loading is a technique in Angular that allows you to load JavaScript components asynchronously when a specific route is activated. It improves the speed of the application load time by splitting the application into several bundles. When the user navigates through the app, the bundles are loaded as required.

Is lazy loading necessary Angular?

Lazy loading is an important Angular feature that helps to reduce the initial load time since it loads only the necessary files first. Other required modules are loaded on demand when you navigate to their particular route. Now, you can take advantage of this feature to improve your app's load time.

What is the router module forRoot method used for?

The forRoot() method creates an NgModule that contains all the directives, the given routes, and the Router service itself. The forChild() method creates an NgModule that contains all the directives and the given routes, but does not include the Router service.


2 Answers

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
}];
like image 86
makman99 Avatar answered Oct 21 '22 23:10

makman99


You can define a preloading strategy, to tell Angular to preload all modules:

import { ...., PreloadAllModules } from '@angular/router'

@NgModule({
  ....
  imports: [
         RouterModule.forRoot(arrayOfYourRoutes, { preloadingStrategy: PreloadAllModules })
  ]

You can also define a custom strategy if you want to preload only some modules. For a good approach to do this, check this : example in angular doc

Edit after your comment, to self describe your module, here's what you can do:

@NgModule(
   imports: [RouterModule.forChild(routesOfFeatureModule)], //use forChild instead of forRoot
   exports: [RouterModule]
)
export class MyFeatureRoutingModule{} //define routing in a separate module of your feature, everything related to routing go there. This way, you doen't polute your main FeatureModule file. Re-export RouterModule to be able to use router directives in your FeatureModule just by importing MyFeatureRoutingModule


@NgModule(
   ...
   imports: [MyFeatureRoutingModule] //import routing of your feature module
)
export class MyFeatureModule{}



@NgModule(
   ...
   imports: [MyFeatureModule] // import your feature module
)
export class AppModule{}
like image 40
Melou Avatar answered Oct 21 '22 22:10

Melou