lazy loading in angular 11 code example
Example 1: how to lazyload angular
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{
path: 'dashboard',
loadChildren: () => import('./modules/dashboard/dashboard.module').then(m => m.DashboardModule)
},
{
path: 'projects',
loadChildren: () => import('./modules/projects/projects.module').then(m => m.ProjectsModule)
},
{ path: '', pathMatch: 'full', redirectTo: 'dashboard' },
];
@NgModule({
imports: [RouterModule.forRoot(routes, { useHash: true })],
exports: [RouterModule]
})
export class AppRoutingModule { }
Example 2: angular lazy loading
content_copy
const routes: Routes = [
{
path: 'customers',
loadChildren: () => import('./customers/customers.module').then(m => m.CustomersModule)
},
{
path: 'orders',
loadChildren: () => import('./orders/orders.module').then(m => m.OrdersModule)
},
{
path: '',
redirectTo: '',
pathMatch: 'full'
}
];
Example 3: creating a module with lazy loading in angular 9
ng g m customers --route customers --module app.module
Example 4: angular lazy loading images
mport { Component } from '@angular/core';@Component({ selector: 'image', template: ` `})class ImageComponent { defaultImage = 'https://www.placecage.com/1000/1000'; image = 'https://images.unsplash.com/photo-1443890923422-7819ed4101c0?fm=jpg';}