Tom
Tom

Reputation: 8681

Lazy loading not working after being configured

I have tried and configured lazy loading in my angular 10 application but the blank page loads. I have written a console.log in the ngOnit of the child component which never fires. Could somebody tell me what is the problem. As you can see below i am trying to lazyload the CustomerOrdersComponent component. I dont see any errors in the console tab and neither a call to the url in the network tab

app.routing.module

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { CustomerDetailsComponent } from '../app/modules/customer/components/customer-details/customer-details.component';

const routes: Routes = [
  { path: 'customers', component: CustomerDetailsComponent },
  { path: 'orders', loadChildren: () => import('./modules/order/order.module').then(m => m.OrderModule) }

];

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

AppModule

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    CommonModule,
    BrowserModule,
    HttpClientModule,
    CustomerModule,
    SharedModule,
    AppRoutingModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Order-routing module

const orderRoutes: Routes = [
  {
    path: 'orders', component: CustomerOrdersComponent
  },
  {
    path: 'orderDetails', component: OrderDetailsComponent
  }
];

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

order module

@NgModule({
  declarations: [
    CustomerOrdersComponent,
    OrderDetailsComponent
  ],
  imports: [
    CommonModule,
    OrderRoutingModule
  ]
})
export class OrderModule { }

CustomerOrders component

export class CustomerOrdersComponent implements OnInit {

  customerOrders: ICustomerOrders[];

  constructor(private customerOrdersService: CustomerOrdersService) { }

  ngOnInit(): void {
    console.log('Order component getting called');
    this.getCustomerOrders();
  }


  getCustomerOrders(): void {
      this.customerOrdersService.getCustomerOrders().subscribe( data => this.customerOrders = data);
  }

}

Upvotes: 1

Views: 2818

Answers (3)

Tsegazaab
Tsegazaab

Reputation: 21

Update your nested Route configuration from:

{
    path: 'orders', component: CustomerOrdersComponent
},

to an empty path:

{
    path: '', component: CustomerOrdersComponent
},

By default, the router looks for an empty path.

Now the validation should work because the path to reach CustomerOrdersComponent is /orders and not orders/orders.

Upvotes: 0

Raz Ronen
Raz Ronen

Reputation: 2628

Update you nested Route configuration from:

{
    path: 'orders', component: CustomerOrdersComponent
},

to an empty path:

{
    path: '', component: CustomerOrdersComponent
},

By default, the router looks for empty path.

Now the validation should work because the path to reach CustomerOrdersComponent is /orders and not orders/orders.

Upvotes: 2

Ayush Singroul
Ayush Singroul

Reputation: 41

Configure your OrderRoutingModule like below

 const orderRoutes: Routes = [    
  {
    path: '', component: CustomerOrdersComponent
  },
  {
    path: 'orderDetails', component: OrderDetailsComponent
  }
];

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

By using the above changes in OrderRoutingModule your routing will start working as expected. Because the first entry point is empty.

Upvotes: 0

Related Questions