DEV Community

Cover image for Migrating to Angular Standalone Components: A Step-by-Step Guide
Nicholas Jones
Nicholas Jones

Posted on

Migrating to Angular Standalone Components: A Step-by-Step Guide

In Angular v14, Standalone components were introduced, marking a significant evolution in the framework’s architecture. With every version upgrade, its functionality was upgraded, and in Angular 19, it was made by default. This will help developers create Angular applications without NgModules, streamlining the development process and improving the application's modularity. This article provides a detailed guide on migrating to Angular standalone components, ensuring a seamless transition while leveraging the advantages of this approach.

What are Standalone Components in Angular?

Standalone components are those Angular components that we can utilize without declaring in NgModules. These components do not rely on NgModules to declare components, directives, and pipes; instead, they directly include all the necessary dependencies in their metadata. This simplifies the application structure, reduces boilerplate, and enhances reusability.

Benefits of Angular Standalone Components:

Angular standalone components provide numerous advantages to modern web development, making them an excellent choice for organizations that want to streamline their workflows. Organizations that want to optimize their projects should hire Angular developer with expertise in standalone components. Their expertise will help ensure efficient development, seamless scalability, and high-performance applications tailored to their business needs. The following are some of the top benefits of Angular standalone components.

1. Simplified Module System:

Standalone components reduce the need for NgModules, which leads to cleaner and more maintainable code.

2. Improved Modularity:

Components can be reused across different parts of the application without dependency constraints.

3. Faster Development:

By eliminating the need to manage complex NgModule hierarchies, you can speed up the development workflows.

4. Future-Ready Architecture:

The standalone components in Angular align with the long-term vision of the Angular team to simplify its ecosystem.

Step-by-Step Guide to Migration

Migrating an Angular project to standalone components involves a structured approach. But before that, you must ensure that your project is compatible with Angular 15.2.0 or later and builds without compilation errors. Additionally, it should be on a clean Git branch with all work saved. The following is a three-step migration process to convert an existing Angular project to standalone components.

1. Convert declaration to standalone:

Use the Run ng g @angular/core:standalone and select "Convert all components, directives, and pipes to standalone". This directive will remove standalone: false from the component decorator and add necessary dependencies to the import array.

Before:

// shared.module.ts
@NgModule({
  imports: [CommonModule],
  declarations: [GreeterComponent],
  exports: [GreeterComponent]
})
export class SharedModule {}
Enter fullscreen mode Exit fullscreen mode
// greeter.component.ts
@Component({
  selector: 'greeter',
  template: '<div *ngIf="showGreeting">Hello</div>',
  standalone: false,
})
export class GreeterComponent {
  showGreeting = true;
}
Enter fullscreen mode Exit fullscreen mode

After:

// shared.module.ts
@NgModule({
  imports: [CommonModule, GreeterComponent],
  exports: [GreeterComponent]
})
export class SharedModule {}
Enter fullscreen mode Exit fullscreen mode
// greeter.component.ts
@Component({
  selector: 'greeter',
  template: '<div *ngIf="showGreeting">Hello</div>',
  imports: [NgIf]
})
export class GreeterComponent {
  showGreeting = true;
}
Enter fullscreen mode Exit fullscreen mode

2. Remove unnecessary NgModules:

Run the ng g @angular/core:standalone schematic again, but select "Remove unnecessary NgModule classes". This removes NgModules that are no longer required after converting declarations to standalone.

// importer.module.ts
@NgModule({
  imports: [FooComponent, BarPipe],
  exports: [FooComponent, BarPipe]
})
export class ImporterModule {}
Enter fullscreen mode Exit fullscreen mode
// importer.module.ts
// Does not exist!
Enter fullscreen mode Exit fullscreen mode

3. Bootstrap the project using standalone APIs:

Run the ng g @angular/core:standalone schematic a final time and choose "Bootstrap the project using standalone APIs". This converts project bootstrapping from bootstrapModule to the new boostrapApplication API. It also removes standalone: false from the root component and removes the root NgModule.

Before:

// ./app/app.module.ts
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
@NgModule({
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {}
Enter fullscreen mode Exit fullscreen mode
// ./app/app.component.ts
@Component({
  selector: 'app',
  template: 'hello',
  standalone: false,
})
export class AppComponent {}
Enter fullscreen mode Exit fullscreen mode
// ./main.ts
import { platformBrowser } from '@angular/platform-browser';
import { AppModule } from './app/app.module';
platformBrowser().bootstrapModule(AppModule).catch(e => console.error(e));
Enter fullscreen mode Exit fullscreen mode

After:

// ./app/app.module.ts
// Does not exist!
Enter fullscreen mode Exit fullscreen mode
// ./app/app.component.ts
@Component({
  selector: 'app',
  template: 'hello'
})
export class AppComponent {}
Enter fullscreen mode Exit fullscreen mode
// ./main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
bootstrapApplication(AppComponent).catch(e => console.error(e));
Enter fullscreen mode Exit fullscreen mode

4. Post-Migration:

After completing the migration process, perform linting and formatting checks to identify and fix any issues that will help ensure code consistency. Commit all the changes to your version control system for safekeeping. Review the project manually to remove residual NgModule declarations not addressed during migration. Execute the unit tests to verify the functionality and resolve any failures. Finally, test the application thoroughly to confirm it functions as expected in all scenarios.

Conclusion:

Migrating to Angular standalone components is a transformative step towards modernizing your application architecture. Standalone components provide a compelling path forward by simplifying module dependencies, enhancing modularity, and aligning with Angular’s evolving ecosystem. By proper planning and following the step-by-step migration process, your application can fully leverage the benefits of this innovative feature. If you want to make the change and improve your Angular development experience, you can get in touch with an Angular development company. Their expertise can help you achieve robust, scalable, and future-ready solutions.

Top comments (0)