Angular 14+ Standalone Components

Ahmed Bouchefra
2 min readOct 3, 2023

--

Hi everyone, and welcome to this video on Angular standalone components. Standalone components are a new feature in Angular that allows you to create reusable components that can be used without the need for an NgModule. This can make your code more modular, efficient, and easier to share.

What are standalone components?

Standalone components are components that are marked with the standalone flag. This flag tells the Angular compiler that the component does not need to be declared in an NgModule. Standalone components can import other standalone components and NgModules, but they cannot export any dependencies.

Benefits of using standalone components

There are several benefits to using standalone components:

  • Modularity: Standalone components can be used in isolation, without the need for an NgModule. This makes your code more modular and easier to reuse.
  • Efficiency: Standalone components are tree-shakable, which means that Angular will only load the components that are actually used in your application. This can improve the performance of your application.
  • Sharing: Standalone components can be shared with other Angular applications. This makes it easy to create reusable components that can be used across multiple projects.

How to create a standalone component

To create a standalone component, simply mark the component with the standalone flag. For example:

@Component({
selector: 'my-standalone-component',
standalone: true,
})
export class MyStandaloneComponent {
// ...
}

Once you have created your standalone component, you can import it into your application and use it like any other component.

Using standalone components in NgModule-based applications

Standalone components can also be used in NgModule-based applications. To do this, you simply need to import the standalone component into your NgModule. For example:

@NgModule({
declarations: [
AppComponent,
MyStandaloneComponent,
],
imports: [
BrowserModule,
],
bootstrap: [AppComponent],
})
export class AppModule {}

Once you have imported the standalone component into your NgModule, you can use it in your application like any other component.

Conclusion

Standalone components are a new and powerful feature in Angular that can make your code more modular, efficient, and easier to share. If you are creating new Angular components, I recommend using the standalone flag whenever possible.

Additional tips for using standalone components

  • Use standalone components for small, reusable components. For larger components, it may be better to use an NgModule.
  • Use standalone components with existing NgModule-based libraries or dependencies. Standalone components can take full advantage of the existing ecosystem of Angular libraries.
  • Consider using Angular Elements to export your standalone components as custom elements. This can make it easy to use your components in other web applications.

I hope this video has been helpful. If you have any questions, please leave a comment below.

--

--

Ahmed Bouchefra
Ahmed Bouchefra

Written by Ahmed Bouchefra

Web developer and technical writer.

Responses (1)