Ankit Raonka
Ankit Raonka

Reputation: 6849

LazyLoad Module in Angular

I am following this blog for Lazy Lodaing a Module

Everything is fine except the part when it asks me to assign static path for the module to be loaded.

the name of the module will depend on the users choice as i may have a number of modules

how can i assign a variable to loadChildren in routing and how to create this variable just before routing

{ path: 'lazy', loadChildren: 'lazy/lazy.module#LazyModule' }

Upvotes: 1

Views: 1333

Answers (2)

Max Koretskyi
Max Koretskyi

Reputation: 105499

One option is to define your custom module loader.

Another option is to use default exports. Since router uses the following code:

  private loadAndCompile(path: string): Promise<NgModuleFactory<any>> {
    let [module, exportName] = path.split(_SEPARATOR);
    if (exportName === undefined) {
      exportName = 'default';
    }

to load a module, you can export default module and don't pass any module name at all:

export default class MyModule {}

Upvotes: 0

Daniel Kucal
Daniel Kucal

Reputation: 9232

If you don't use Ahead of Time compilation you can try passing a function to loadChildren:

{ path: 'lazy', loadChildren: () => 'lazy/lazy.module#' + dynamicModuleName }

Upvotes: 1

Related Questions