Srikanth Reddy
Srikanth Reddy

Reputation: 545

Compile error for Lazy loading in angular

I'm working on lazy loading in angular. i had created a new module named "Offers", in that folder itself I had created the component named "Offers".

Folder Structure Reference

app-routing.module.ts :

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { OrdersComponent } from './orders/orders.component';
const routes: Routes = [
 { path:"" , component:HomeComponent},
 { path:"Home" , component:HomeComponent},
 { path:"Orders" , component:OrdersComponent},
 {path:"Offers", loadChildren:'./Offers/Offers.module'},
];

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

I had used loadchildred to load the lazy module(Offers) which needs to display on demand.

Offers-routing.module.ts :

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { OffersComponent } from '../offers/offers.component';


const routes: Routes = [
  {path:"" , component:OffersComponent}
];

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

appcomponent.html :

<div style="text-align:center">
<a [routerLink]="['/']">Products</a>
<a [routerLink]="['/Orders']">My Orders</a>
<a [routerLink]="['/Offers']">Offers</a>
</div>
<div>
<router-outlet>

</router-outlet>
</div>

Currently on compile, I'm getting the following error :

"ERROR in ./src/app/Offers/Offers.module.ts Module build failed (from ./node_modules/@ngtools/webpack/src/index.js): Error: G:\AngularTest\Lazyload\src\app\Offers\Offers.module.ts is missing from the TypeScript compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property. at AngularCompilerPlugin.getCompiledFile (G:\AngularTest\Lazyload\node_modules@ngtools\webpack\src\angular_compiler_plugin.js:913:23) at G:\AngularTest\Lazyload\node_modules@ngtools\webpack\src\loader.js:41:31 at processTicksAndRejections (internal/process/task_queues.js:93:5)"

Upvotes: 1

Views: 1283

Answers (1)

G&#233;r&#244;me Grignon
G&#233;r&#244;me Grignon

Reputation: 4228

You need to fix the expected syntax on loadChildren :

{path:"Offers", loadChildren:'./offers/offers.module#OffersModule'}

With newer apps and the official docs, you might find the new syntax with javascript dynamic import :

{
  path:"Offers",
  loadChildren: () => import('./offers/offers.module').then(m => m.OffersModule)
}

Upvotes: 2

Related Questions