Loading episodes…
0:00 0:00

Generate standalone components in Angular 14

00:00
BACK TO HOME

Generate standalone components in Angular 14

ahmed June 07, 2022 2 min read

In this example, we’ll learn how to generate angular 14 standalone components.

Stand-alone components were added in Angular 14 as a means of reducing reliance on NgModules. Since it is currently in the developer preview stage, you shouldn’t use it for any apps that are designed for production.

On the other hand, you are welcome to give it a try and share your thoughts with the Angular team through their GitHub repository.

The command ng generate component <name> --standalone makes it easy to generate a component that is independent of any module in your application.

First and foremost, stand-alone components provide you the ability to directly add imports (components, directives, pipes, and modules) to the @component decorator without having to make use of @NgModule.

Open a command line interface and simply run the following command from within an angular 14 project:

ng g component footer --standalone

You can then access the component’s code from the src/app/footer/footer.component.ts file.

Update the component as follows:

import { Component } from ‘@angular/core;
import { MatButtonModule } from '@angular/material/button';

@Component({  
  selector: 'app-footer',  
  standalone: true,  
  
  imports: [    
    CommonModule, 
    MatButtonModule
  ],
  templateUrl: './footer.component.html',
  styleUrls: ['./footer.component.scss']
})
export class FooterComponent {}

We imported the MatButtonModule directly via the imports array of the component’s decorator.


Join the 10xdev Community

Subscribe and get 8+ free PDFs that contain detailed roadmaps with recommended learning periods for each programming language or field, along with links to free resources such as books, YouTube tutorials, and courses with certificates.

Audio Interrupted

We lost the audio stream. Retry with shorter sentences?