1

Note: I've searched a dozen other related questions regarding lazy loading on SO, but I wasn't able to solve my issue using them.

Error: Cannot find 'ContactModule' in 'app/contact.module'

I have started with a new project based on Angular CLI and the node console in Windows mentions webpack: Compiling... occasionally so I believe I'm using that, however I do not see any webpack.config.js in my project which other questions mentioned needing changes to.

Here's my app.module.ts:

const appRoutes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'contact', loadChildren: 'app/contact.module#ContactModule' },
];

//...imports...
@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    // ContactComponent,
    // ContactFormComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    JsonpModule,
    RouterModule.forRoot(appRoutes)
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

My sub module for lazy loading:

//...imports...
const ROUTES = [
  { path: '', component: ContactComponent },
];

@NgModule({
    declarations: [
        ContactComponent,
        ContactFormComponent
    ],
  imports: [RouterModule.forChild(ROUTES)],
})
class ContactModule {}

And some images of my folder structure and packages.json in case those are helpful. enter image description here enter image description here

2 Answers 2

1

First of all you should export your ContactModule:

export class ContactModule {}
Sign up to request clarification or add additional context in comments.

Comments

1

Took me a while to figure this out since many of the answers I came across didn't have it in their example...

The lazy loaded module needs to have export in the class and CommonModule needs to be imported as well as any other things you are using such as forms.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.