Angular standalone components

A simplified way to build Angular applications

July 3, 2023 | 5 min read
avatar
Ankit Pant
Senior Frontend Engineer - R&D

Introduction

Until now, developers have been using NgModules to specify the components, directives, pipes, etc that are available for use in templates, however, after the release of Angular @Component({ selector: '...', templateUrl: '...', styleUrls: ['...'], standalone: true, imports: [ NgIf, NgFor, AsyncPipe, ... ], }) export class FooBarComponent {

Generate a standalone component via the Angular CLI

ng generate component --standalone account-statements

A component generated with --standalone flag is not added to NgModule and contains default imports such as CommonModule to get started with the component right away.

Adopt standalone components incrementally

Standalone components can be seamlessly combined with existing modules. They are easy to implement, and can be adopted incrementally into an app. A standalone component, pipe or directive can also be imported into an existing NgModule configuration, applied incrementally for complex apps, so that you can ensure that there are no breaking changes.

@NgModule({
  declarations: [...],
  exports: [...]
  imports: [
    AccountDetailsComponent, // This is a standalone component
    CommonModule,
    RouterModule.forChild(routes),
    .....
  ],
})

Migrate a component to a Standalone

You can convert a component in one of the following ways:

Continue the Journey: Explore Related Posts

Styling your legacy Micro Frontends August 8, 2024 | 5 min read

Different approaches to style legacy Micro Frontends.

avatar
Pablo Villoslada Puigcerber
Read more
Maintaining legacy code with Micro Frontends May 15, 2024 | 7 min read

Taking advantage of Micro Frontends with Module Federation to maintain legacy applications.

avatar
Pablo Villoslada Puigcerber
Read more