angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Lazy Loaded Module Inside Another Module Imported w/ .forRoot() Doesn't Load

Open brtnshrdr opened this issue 9 years ago • 5 comments

Please provide us with the following information:

OS?

Windows 7, 8 or 10. Linux (which distribution). Mac OSX (Yosemite? El Capitan?) macOS Sierra

Versions.

Please run ng --version. If there's nothing outputted, please run in a Terminal: node --version and paste the result here: angular-cli: 1.0.0-beta.26 node: 6.5.0 os: darwin x64 @angular/common: 2.4.5 @angular/compiler: 2.4.5 @angular/core: 2.4.5 @angular/flex-layout: 2.0.0-beta.3 @angular/forms: 2.4.5 @angular/http: 2.4.5 @angular/material: 2.0.0-beta.1 @angular/platform-browser: 2.4.5 @angular/platform-browser-dynamic: 2.4.5 @angular/router: 3.4.5 @angular/compiler-cli: 2.4.5

Repro steps.

Was this an app that wasn't created using the CLI? What change did you do on your code? etc.

Create a module with routes, including a lazy loaded route

export const exampleRouting = RouterModule.forChild([
  {
    path : 'examples', component : DefaultLayoutComponent, children : [
    {path : '', redirectTo : 'dashboard', pathMatch: 'full'},
    {path : 'dashboard', component : DashboardComponent},
    {path : 'fake', component : FakeComponent},
    {path : 'lazy', loadChildren : './lazy/lazy.module#LazyModule'},
  ]
  }
]);

Have your module need a .forRoot() function

@NgModule({
  imports : [
    exampleRouting,
    ...other stuff
  ],
  declarations : [DashboardComponent, FakeComponent],
})
export class ExamplesModule {
  static forRoot(): ModuleWithProviders {
    return {
      ngModule: ExamplesModule,
      providers: [MyService]
    }
  }
}

Import this ExamplesModule into your AppModule like so:

imports: [ExamplesModule.forRoot()]

The log given by the failure.

When trying to navigate to the lazy loaded route, I get the following error:

Unhandled Promise rejection: Cannot find module './lazy/lazy.module'. ; Zone: angular ; Task: Promise.then ; Value:

brtnshrdr avatar Jan 27 '17 02:01 brtnshrdr

@brtnshrdr Hi! Try to remove './' from the loadChildren path and add the full path, something like app/lazy/lazy.module#LazyModule

rokikon avatar Jan 30 '17 14:01 rokikon

I changed the lazy loaded route to: {path : 'lazy', loadChildren : 'app/examples/lazy/lazy.module#LazyModule'},

and still get the error:

Error: Cannot find module 'app/examples/lazy/lazy.module.ngfactory'.

brtnshrdr avatar Jan 30 '17 17:01 brtnshrdr

I'm seeing this now since upgrading from beta.21-1 to beta.26. It's not generating the lazy loaded module bundles anymore.

My route format is as suggested.

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

andyrue avatar Jan 30 '17 18:01 andyrue

I believe the core reason for this was https://github.com/angular/angular-cli/issues/2496. It has been fixed meanwhile (after beta.26).

filipesilva avatar Feb 03 '17 15:02 filipesilva

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.