Components are the basic building blocks of Angular applications. A component is a self-contained code unit that has associated view templates and styles. Components can be used in Angular without a module. This is known as a standalone component.
In this artice , i am going to create an angular standalone component.
The idea behind angular standalone component is to make it easy to reuse and reason about code. When you create a standalone component, you are creating a self-contained unit of functionality that can be used in any Angular application.
There are many benefits to using standalone components in your Angular applications.Angular Standalone components can improve the modularity and testability of your code, and they can make your code more maintainable in the long run.
Angular 14 introduces an alternative method of writing apps, standalone components, directives, and pipes.
When working with Angular, you may find yourself needing to create a standalone component. This is a simple process that can be done in just a few steps.
Learning Objective
- Steps to create angular standalone component and find the code on github
- Angular standalone component lazy loading
- Standalone flag and component imports
- Using node module in standalone component
- Standalone component advantages
- Conclusion
Steps to create angular standalone component
First create a new application having angular cli 14.
import { Injectable } from '@angular/core';
export interface Book {
name: string;
id: number;
description: string;
imageUrl: string;
}
@Injectable({
providedIn: 'root'
})
export class BooksService {
books: Book[] = [
{
name: 'Book 1',
id: 1,
description: 'Book description 1',
imageUrl: 'https://cdn.pixabay.com/photo/2015/06/02/12/59/book-794978_960_720.jpg'
},
{
name: 'Book 2',
id: 2,
description: 'Book description 2',
imageUrl: 'https://cdn.pixabay.com/photo/2015/06/02/12/59/book-794978_960_720.jpg'
},
{
name: 'Book 3',
id: 3,
description: 'Book description 3',
imageUrl: 'https://cdn.pixabay.com/photo/2015/06/02/12/59/book-794978_960_720.jpg'
},
{
name: 'Book 4',
id: 4,
description: 'Book description 4',
imageUrl: 'https://cdn.pixabay.com/photo/2015/06/02/12/59/book-794978_960_720.jpg'
}
];
constructor() { }
}
import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { BooksService } from './books.service';
import { RouterModule } from '@angular/router';
import {MatButtonModule} from '@angular/material/button';
@Component({
selector: 'app-book-list',
standalone: true,
imports: [CommonModule, RouterModule, MatButtonModule],
template: `
<section>
<div class="grid-container">
<ng-container *ngFor="let book of bookService?.books; let i = index">
<div class="grid-item" [routerLink]="'/details/' + i">
<h3>{{ book?.name }}</h3>
<img width="200" height="200" [src]="book?.imageUrl" [alt]="book?.name"/>
<div class="">
<button mat-raised-button>Add to cart</button>
</div>
</div>
</ng-container>
</div>
</section>
`,
styles: [
`
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
background-color: #2196f3;
padding: 10px;
}
.grid-item {
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid rgba(0, 0, 0, 0.8);
padding: 20px;
cursor: pointer;
font-size: 30px;
text-align: center;
}
.cart-button {
margin-bottom: 20px
}
.cart-button button {
margin-top: 20px;
}
`,
],
})
export class BookListComponent implements OnInit {
constructor(readonly bookService: BooksService) { }
ngOnInit(): void { }
}
Angular standalone component lazy loading
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{ path: '', pathMatch: 'full', redirectTo: 'book-list' },
{ path: 'book-list', loadComponent: () => import('./book-list.component').then(mod => mod.BookListComponent)},
{ path: 'details/:index', loadComponent: () => import('./book-view.component').then(mod => mod.BookViewComponent) }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
Standalone flag and component imports
@Component({
standalone: true,
selector: 'book-list',
imports: [BookListComponent],
template: `
...
`,
})
export class BookListComponent {
// component logic
}
Using node module in standalone component
Standalone component advantages
- The learning experience benefits from removing the element of complexity at lower entries.
- This is intended to quicken build times.
- Angular standalone component still permits to be lazy load.
- You can even use NgModule-based components and standalone ones with a zoneless technique. Collaborating to decide when to introduce this innovation is not difficult, but there is no rationale to realize this suggestion at present.
- There are no implied dependencies.
- Less boilerplate to write means there are fewer files.
Find this code on Github
0 Comments