7

I am using Angular 5.2.10 and trying to make application to lazy loading.so creating new module for AuditorloginComponent and i have removed my AuditorloginComponent from app.module.ts

auditorlogin.module.ts

@NgModule({
  imports: [
    CommonModule,
    AuditorloginRoutingModule,FormsModule
  ],
  declarations: [AuditorloginComponent]
})
export class AuditorloginModule { } 

auditorlogin-routing.module.ts

const routes: Routes = [
  { 
    path: '',
    component: AuditorloginComponent
}
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class AuditorloginRoutingModule { }

app.routing.ts

I have added loadChildren

 {
    path: 'auditorlogin',
    loadChildren: 'app/auditor/auditorlogin/auditorlogin-routing.module#AuditorloginRoutingModule',
    pathMatch: 'full'
  },

But i am getting

core.js:1449 ERROR Error: Uncaught (in promise): Error: Component AuditorloginComponent is not part of any NgModule or the module has not been imported into your module.

Error: Component AuditorloginComponent is not part of any NgModule or the module has not been imported into your module

Kindly tell to me where is my mistake?

1
  • Tried not working @Faisal Commented Apr 23, 2018 at 6:07

5 Answers 5

8

you don't have your component in AuditorloginRoutingModule declarations:[...]

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @dee zg got it my mistake
Don't think, this will work, because, it will throw error like ' the component is part of the declarations of 2 modules'
4

Your problem is in AuditorloginRoutingModule . you must be added AuditorloginComponent in declration like this

const routes: Routes = [
  { 
    path: '',
    component: AuditorloginComponent
}
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
  declarations: [
    // components that are used will be added here.
    AuditorloginComponent]
})
export class AuditorloginRoutingModule { }

Comments

3

You are only lazy loading the routing module, while you should load the AuditorloginModule which then uses the AuditorloginRoutingModule for routing and not the other way round.

{
    path: 'auditorlogin',
    loadChildren: 'app/auditor/auditorlogin/auditorlogin.module#AuditorloginModule',
    pathMatch: 'full'
},

1 Comment

This is working for me.. I think.. this should be accepted answer
1

Change AuditorloginModule to

@NgModule({
  imports: [
    CommonModule,
    AuditorloginRoutingModule,FormsModule
  ],
  declarations: [AuditorloginComponent],
  export: [AuditorloginComponent]
})
export class AuditorloginModule { } 

And then declare AuditorloginComponent to any module which you want to use it in.

4 Comments

had you imported AuditorloginModule to the target module ?
Why imported AuditorloginModule to app.module.ts?
If i will import AuditorloginModule to app.module.ts.its will not generated AuditorloginModule.chunk.js files
I didn't mean to app.module.ts , For example if you want to share X module for Z and Y module , you should import X for both modules and export X components in X module.
0

Make it sure that your component is imported in app-routing.module.ts. In my case that it was the reason where I forgot to include the component in the deceleration

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    IndexComponent
  ],
  imports: [
    BrowserModule, FormsModule, HttpClientModule, RouterModule.forRoot(routes)
  ],
  exports: [RouterModule],
  providers: [],
  bootstrap: [AppComponent]
})

Also make sure you restart server when new modules and components are added while ng serve it is also a reaosn when this problem occur. So, the basic solution is to quit the server and ng serve again.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.