Search code examples
angularangular-routingangular-lazyloading

Angular lazy-loading implementation for NavMenuComponent component


I have implemented lazy-loading feature in my project by following the approach on official Lazy-loading feature module page. However, I have not idea aboıut how can I treat the modules or components that will not be directed as NavMenuComponent, Sidebar, etc. On the other hand, should I set path: '', for HomeComponent or create a new route for Home?

Here is the points that I confused:

1. I think I do not need to create navmenu-routing.module.ts and navmenu.module.ts as I created for the other routes. Is that true?

2. In the app.module, I defined my NavMenuComponent as shown below. But I am not sure if I should move NavMenuComponent to declarations field?

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

3. In app-routimg.module, I do not use NavMenuComponent. Is it correct?

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

const routes: Routes = [
  {
    path: 'customers',
    loadChildren: () => import('./counter/customer.module').then(m => m.CustomerModule)
  },
  {
    path: 'orders',
    loadChildren: () => import('./order/order.module').then(m => m.OrderModule)
  },
  {
    path: '',
    redirectTo: '',
    pathMatch: 'full'
  }
];

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

Is there any mistake or anything that I should check?


Solution

  • Not really clear about the question, but I sense you are trying to make routing lazy loading work

    1. Your app module and routing module looks correct

    2. Now in the order module, you will have an order-routing.module.ts file where you can define a path such as

      const routes: Routes = [
          {path:'list', component: ListOrdersComponent}
        ];
    
    1. Now in your nav component you can call the route /order/list which will show the ListOrdersComponent in the router-outlet.