Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Lazy Loading ( the module has not been imported into your module)

Tags:

angular

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?

like image 251
Vikram R Avatar asked Apr 23 '18 05:04

Vikram R


People also ask

What is lazy loaded module Angular?

Lazy loading is the process of loading components, modules, or other assets of a website as they're required. Since Angular creates a SPA (Single Page Application), all of its components are loaded at once. This means that a lot of unnecessary libraries or modules might be loaded as well.

How lazy loading works in Angular?

Lazy loading is a technique in Angular that allows you to load JavaScript components asynchronously when a specific route is activated. It improves the speed of the application load time by splitting the application into several bundles. When the user navigates through the app, the bundles are loaded as required.

How do I know if Angular lazy loading is working?

If you want to check how lazy loading works and how lazy loading routing flow, then Augury is the best tool we have. Click on ctrl+F12 to enable the debugger and click on the Augury tab. Click on the router tree. Here, it will show the route flow of our modules.

How do you get lazy loading in Angular 8?

To lazy load Angular modules, use loadChildren (instead of component ) in your AppRoutingModule routes configuration as follows. content_copy const routes: Routes = [ { path: 'items', loadChildren: () => import('./items/items. module'). then(m => m.


4 Answers

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

like image 103
dee zg Avatar answered Sep 21 '22 01:09

dee zg


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 { }
like image 37
Mohammad Daliri Avatar answered Sep 23 '22 01:09

Mohammad Daliri


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'
},
like image 24
Fussel Avatar answered Sep 21 '22 01:09

Fussel


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.

like image 28
Arash Avatar answered Sep 24 '22 01:09

Arash