简体   繁体   中英

Lazy load module outside src Angular CLI

I'm working in a larger project where we need to store modules outside an application. Anyone know if it's possible to lazy load a module that's stored outside the src folder?

Example:

./
 - src/
   - app/
     - app.router.ts
       ...
 - +lazy/
   - lazy.module.ts
   - lazy.component.ts
   - lazy.router.ts

In app.router.ts:

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

I found a solution, keep in mind that the lazy loaded module need to have a default export.

export default class LazyModule { }

Router:

{
    path: '',
    loadChildren: () =>
        System.import('../+lazy/lazy.module').then((m: any) => {
            return m.default;
        })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM