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 { }
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.
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With