MPTPR 19
MPTPR 19
-:Practical 19:-
Aim: Design a page to load customer and Sales order data using lazy loading
technique in angular.
Lazy loading is a technique used in Angular to optimize the loading time of large applications
by loading only the necessary components and modules when they are required, rather than
loading everything upfront.
In a typical Angular application, all the components and modules are loaded when the
application is first launched. This can slow down the initial loading time of the application,
especially if the application is large and contains many components and modules. With lazy
loading, the application can be divided into smaller feature modules, each of which is loaded
only when the user navigates to the corresponding route.
Step 1: Generate the customer module: Run the following command in your terminal to
generate a new module for customers:
@NgModule({
declarations: [
CustomerListComponent
],
imports: [
CommonModule,
CustomersRoutingModule
]
})
export class CustomersModule { }
Step 3: Generate the customer list component: Run the following command in your
terminal to generate a new component for displaying the customer list:
@Component({
selector: 'app-customer-list',
templateUrl: './customer-list.component.html',
styleUrls: ['./customer-list.component.css']
})
export class CustomerListComponent implements OnInit {
customers: any[] = [];
isLoading = false;
ngOnInit() {
this.loadCustomers();
}
loadCustomers() {
this.isLoading = true;
this.dataService.getCustomers().subscribe((customers: any[]) => {
this.customers = customers;
this.isLoading = false;
});
}
}
<div *ngIf="isLoading">
Loading customers...
</div>
<div *ngIf="!isLoading">
<h2>Customers</h2>
<ul>
<li *ngFor="let customer of customers">{{ customer.name }}</li>
</ul>
</div>
Step 5: Generate the sales module: Run the following command in your terminal to
generate a new module for sales:
@NgModule({
declarations: [
SalesOrderListComponent
],
imports: [
CommonModule,
SalesRoutingModule
]
})
export class SalesModule { }
Step 7: Generate the sales order list component: Run the following command in your
terminal to generate a new component for displaying the sales order list:
@Component({
selector: 'app-sales-order-list',
templateUrl: './sales-order-list.component.html',
styleUrls: ['./sales-order-list.component.css']
})
export class SalesOrderListComponent implements OnInit {
salesOrders: any[] = [];
isLoading = false;
Compiled by: AHH Page
Modern Practical Tools(4340705) 20______
ngOnInit() {
this.loadSalesOrders();
}
loadSalesOrders() {
this.isLoading = true;
this.dataService.getSalesOrders().subscribe((salesOrders: any[]) => {
this.salesOrders = salesOrders;
this.isLoading = false;
});
}
}
<div *ngIf="isLoading">
Loading sales orders...
</div>
<div *ngIf="!isLoading">
<h2>Sales Orders</h2>
<ul>
<li *ngFor="let order of salesOrders">{{ order.orderNumber }}</li>
</ul>
</div>
Step 10: Create a data service to fetch the customer and sales order data: Run the
following command in your terminal to create a new service:
Step 11: This will create a new file called data.service.ts in the src/app directory. Replace
its content with the following code:
@Injectable({
providedIn: 'root'
})
export class DataService {
getCustomers(): Observable<any[]> {
// Replace with the actual logic to fetch customer data
return of([
{ id: 1, name: 'Janak' },
{ id: 2, name: 'Rekha' },
{ id: 3, name: 'Mayank' },
{ id: 4, name: 'Pratibha' },
{ id: 5, name: 'Vinayak' }
]);
}
getSalesOrders(): Observable<any[]> {
// Replace with the actual logic to fetch sales order data
return of([
{ orderNumber: 'SO-001', total: 100 },
{ orderNumber: 'SO-002', total: 200 },
{ orderNumber: 'SO-003', total: 300 },
{ orderNumber: 'SO-004', total: 400 },
{ orderNumber: 'SO-005', total: 500 }
]);
}
}
Step 12: Update the routing configuration to enable lazy loading: Open the app-
routing.module.ts file and update it as follows:
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Step 13: Open the app.component.html file and replace its content with the following
line:
<router-outlet></router-outlet>