*
Previous Parent Child Data passing in React Angular-Project-Manifest Next

Angular Standalone Components

πŸ”Ή 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.

βœ… Key Features of Standalone Components

  • No need for NgModules – Components, directives, and pipes can now exist independently.
  • Imports Array – Dependencies are declared directly inside the component.
  • Simpler App Bootstrapping – Use bootstrapApplication() instead of AppModule.
  • Better Tree-Shaking – Smaller bundle size by including only used components.
  • Lazy Loading Made Easy – Routes can directly reference standalone components.

πŸ”‘ Key Differences from Traditional Components

  • Decoupled from NgModules: No need to declare in an NgModule’s declarations array.
  • Direct Dependencies: Use the imports array to declare dependencies.
  • Simplified Bootstrapping: Use bootstrapApplication() instead of an AppModule.

βœ… Benefits of Using Standalone Components

  • Reduced Boilerplate: No need to declare components in NgModule. Eliminates the overhead of managing declarations, imports, and exports across multiple modules. Perfect for small utilities, feature components, or micro frontends.
  • Improved Tree-Shaking: Only the explicitly imported dependencies are bundled. Angular’s build process can more effectively eliminate unused code. Results in smaller bundle sizes and faster load times
  • Enhanced Reusability: Portable and self-contained.You can reuse them across different apps or modules without tight coupling. Ideal for building shared libraries or design systems
  • Simplified Lazy Loading: Direct routing to components without feature modules.No need to create a separate lazy-loaded module. Makes route-based code splitting cleaner and more intuitive
  • Easier Testing: Isolated setup with minimal dependencies. No need to mock or configure unrelated module-level dependencies
  • 🧠 Clear Separation of Concerns : Promotes the Single Responsibility Principle. Each component focuses on its own logic, template, and dependencies. Easier to maintain, debug, and document.
  • πŸ› οΈ Incremental Adoption : You don’t need to refactor your entire app. Standalone components can coexist with traditional NgModule-based components. Angular CLI even provides migration tools to help transition gradually.

βœ… Syntax

@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 {}

πŸ”Ή Example: Standalone Component in Angular

1. main.ts (bootstrapping without AppModule)

import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';

bootstrapApplication(AppComponent)
  .catch(err => console.error(err));

2. app.component.ts (Standalone root component)

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 {}

3. counter.component.ts (Another standalone component)

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--;
  }
}

πŸ”Ή How it Works

  • main.ts bootstraps the AppComponent directly.
  • AppComponent is standalone and imports CounterComponent.
  • CounterComponent manages its own logic and template independently.
  • No NgModule is involved at any point.

πŸ§ͺ Another Example: Creating a Standalone Component

1. Generate the Component

ng generate component greeting --standalone

2. Component File

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!';
}

3. Using the 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';
}

πŸ“Œ Best Practices

  • Start new projects with standalone: Simplifies structure from day one.
  • Use incrementally: Mix with NgModules during gradual migration.
  • Use migration tool: Run ng generate @angular/core:standalone to convert existing components.

βœ… Summary

Standalone Components simplify Angular development by removing the dependency on NgModules, making the codebase cleaner, easier to understand, and more modular.

Back to Index
Previous Parent Child Data passing in React Angular-Project-Manifest Next
*
*