Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lazy loading feature modules with multiple components doesn't work in angular 6

I have a customer module with customer-routing module:

const routes: Routes = [
  {
    path: '', component: CustomerComponent,
    children: [
      { path: 'edit', component: EditCustomerComponent }
    ]
  }
];

And this is my app-routing module:

const routes: Routes = [
  { path: 'customers/:id', loadChildren: './customer/customer.module#CustomerModule' },
  { path: 'login', component: LoginComponent}
];

But when I follow such path customers/3/edit it shows me always CustomerComponent not EditCustomerComponent.

Maybe lazy loading doesn't work?

PS: I am using angular 6.1.0

Update: My customer module

import {ReactiveFormsModule} from '@angular/forms';
import {CustomerComponent} from './customer.component';
import {EditCustomerComponent} from './edit-customer.component';

@NgModule({
  imports: [
    CommonModule,
    CustomerRoutingModule,
    ReactiveFormsModule
  ],
  declarations: [CustomerComponent, EditCustomerComponent]
})
export class CustomerModule { }
like image 361
Aydar Omurbekov Avatar asked Sep 23 '18 10:09

Aydar Omurbekov


People also ask

How does lazy loading work in Angular 6?

Angular 6 allows to Lazy load modules when they are required, they are not loaded at once on application initialization. To implement Lazy loading in Angular 6 we will use the main Routing module which will import all components we want to lazy load.

How many component can be lazy loaded in an app?

The bundle sizes for Lazy 1 and 2 are different, too. Lazy 1 only has a single component, so it is smaller than Lazy 2 (which contains three components).

Can component be lazy loaded in Angular?

You can lazily load a component in any other component, hence creating a parent-child relationship between them. You want to lazy load GreetComponent on the click of the button in the parent component, so to do that add a button as shown next.

What are the disadvantages of lazy loading in Angular?

Disadvantages of Lazy loading Firstly, the extra lines of code, to be added to the existing ones, to implement lazy load makes the code a bit complicated. Secondly, lazy loading may affect the website's ranking on search engines sometimes, due to improper indexing of the unloaded content.


1 Answers

You don't have to put edit route under children. Your customer-routing module would simply look like this:

const routes: Routes = [
  { path: '', component: CustomerComponent },
  { path: 'edit', component: EditCustomerComponent }
];

Also make sure to check whether this routes array has been imported using forChild function in your customer-routing module like this:

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

Hope this should solve your routing issue.

like image 97
Syed Sharique Avatar answered Oct 02 '22 10:10

Syed Sharique