Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I lazy load an external module from node_modules?

Tags:

angular

We can lazy load a local module like this module

    {
        path: 'somePpath',
        loadChildren: 'app/path/some.module#SomeModule'
    },

How do we lazy load a module that comes from an external library resides in node_modules?

like image 507
user1532043 Avatar asked May 17 '17 12:05

user1532043


1 Answers

To load an external module in the router module you might need to use a Wrapper module. Create a wrapper module in the same local project in which you are having your routing module. Import your external module in this wrapper module with traditional import syntax.

import { SomeModule } from '@externalLib';

Include this module in the imports of NgModule's import array.

@NgModule({
  imports: [SomeModule]
})
export class SomeWrapperModule {
}

Then use this wrapper module in the router module as we usually use a module.

// for Angular 7 and below 
{
    path: 'some-path',
    loadChildren: '../somewrapper.module#SomeWrapperModule'
}

// for Angular 8+
{
    path: 'some-path',
    loadChildren: () => import('../somewrapper.module').then(mod => mod.SomeWrapperModule)
}
like image 51
Nilesh suryanarayan Avatar answered Oct 09 '22 07:10

Nilesh suryanarayan