| Parent Child Data passing in React | Angular-Project-Manifest | |
Angular Standalone Components |
Standalone components are Angular components that are self-contained and don't require an NgModule to be declared. This feature simplifies application development by reducing boilerplate and promoting modular, flexible architecture.
Standalone components, directives, and pipes declare their dependencies directly within their @Component decorator.
Traditionally in Angular, every component had to belong to an NgModule. This made the architecture more complex because you had to declare every component, directive, and pipe in an NgModule before using them.
With Angular 14 (and onwards), Angular introduced Standalone Components.
A Standalone Component is a component that is self-contained and does not require an NgModule to be declared or used.
bootstrapApplication() instead of AppModule.declarations array.imports array to declare dependencies.bootstrapApplication() instead of an AppModule.@Component({
selector: 'app-example',
standalone: true, // π This makes it standalone
imports: [], // π You declare dependencies here
templateUrl: './example.component.html',
styleUrls: ['./example.component.css']
})
export class ExampleComponent {}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent)
.catch(err => console.error(err));
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CounterComponent } from './counter.component';
@Component({
selector: 'app-root',
standalone: true, // π Standalone root
imports: [CommonModule, CounterComponent], // π Import dependencies/components
template: `
<h1>π Angular Standalone Components Example</h1>
<app-counter></app-counter>
`
})
export class AppComponent {}
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-counter',
standalone: true, // π Standalone
imports: [CommonModule],
template: `
<h2>Counter: {{ count }}</h2>
<button (click)="increment()">Increment</button>
<button (click)="decrement()">Decrement</button>
`
})
export class CounterComponent {
count = 0;
increment() {
this.count++;
}
decrement() {
this.count--;
}
}
AppComponent directly.AppComponent is standalone and imports CounterComponent.CounterComponent manages its own logic and template independently.NgModule is involved at any point.ng generate component greeting --standalone
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-greeting',
standalone: true,
imports: [CommonModule],
template: `
<div class="greeting-card">
<p>{{ message }}</p>
</div>
`,
styles: `
.greeting-card {
border: 1px solid #ccc;
padding: 16px;
border-radius: 8px;
background-color: #f9f9f9;
}
`
})
export class GreetingComponent {
message = 'Hello from a standalone component!';
}
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
import { GreetingComponent } from './greeting/greeting.component';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet, GreetingComponent],
template: `
<main>
<h1>My Standalone App</h1>
<app-greeting></app-greeting>
</main>
<router-outlet></router-outlet>
`,
styleUrl: './app.component.css'
})
export class AppComponent {
title = 'my-standalone-app';
}
ng generate @angular/core:standalone to convert existing components.Standalone Components simplify Angular development by removing the dependency on NgModules, making the codebase cleaner, easier to understand, and more modular.
| Parent Child Data passing in React | Angular-Project-Manifest | |