• Technologies
  • AI Tips and Tricks
  • Frontend
  • Backend
  • Server
  • Contact
  • About
Wednesday, May 14, 2025
Adnan Halilovic Blog
  • Technologies
    • All
    • Angular
    • Git
    • JavaScript
    • ReactJS
    VS Code Setup for Angular Developers Featured Image

    Best VS Code Setup for Angular Development

    Mastering DevTools: Supercharge Your Workflow with Code Snippets!

    Mastering DevTools: Supercharge Your Workflow with Code Snippets!

    Quokka.js and Wallaby.js Giveaway

    Quokka.js and Wallaby.js Giveaway

    How to Work on Different Git Branches at the Same Time?

    How to Work on Different Git Branches at the Same Time?

    Lazyload Standalone Components in Angular

    Lazyloading Standalone Components in Angular

    Standalone Components Angular

    How to Create Standalone Components in Angular

    Private Routes in React Router V6

    Protecting Routes in React JS

    How to setup routing in ReactJS

    How to setup routing in ReactJS

    Retry Error HTTP Requests in Angular (without retryWhen)

    Retry Error HTTP Requests in Angular (without retryWhen)

  • AI Tips and Tricks
  • Frontend
  • Backend
  • Server
  • Contact
  • About
No Result
View All Result
  • Technologies
    • All
    • Angular
    • Git
    • JavaScript
    • ReactJS
    VS Code Setup for Angular Developers Featured Image

    Best VS Code Setup for Angular Development

    Mastering DevTools: Supercharge Your Workflow with Code Snippets!

    Mastering DevTools: Supercharge Your Workflow with Code Snippets!

    Quokka.js and Wallaby.js Giveaway

    Quokka.js and Wallaby.js Giveaway

    How to Work on Different Git Branches at the Same Time?

    How to Work on Different Git Branches at the Same Time?

    Lazyload Standalone Components in Angular

    Lazyloading Standalone Components in Angular

    Standalone Components Angular

    How to Create Standalone Components in Angular

    Private Routes in React Router V6

    Protecting Routes in React JS

    How to setup routing in ReactJS

    How to setup routing in ReactJS

    Retry Error HTTP Requests in Angular (without retryWhen)

    Retry Error HTTP Requests in Angular (without retryWhen)

  • AI Tips and Tricks
  • Frontend
  • Backend
  • Server
  • Contact
  • About
No Result
View All Result
Adnan Halilovic Blog
No Result
View All Result

How to Create Standalone Components in Angular

Adnan Halilovic by Adnan Halilovic
September 10, 2022
in Angular, Frontend, Technologies
0
0
SHARES
200
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

With the introduction of standalone components in Angular versions 14 and later, building Angular applications is simplified.


As the feature is still in developer preview, we will test it to determine its functionality, but be aware that it may undergo changes before it becomes stable.

What has changed?

Creating a standalone component is surprisingly simple. The only thing we must do is add the --standalone flag when creating the component via the CLI. So in the next steps, we are going to see it in action.


Generating a component

With the following command, we are creating a new component named standalone. (The name of the component could be anything else, but I just used the name standalone because of the simplicity).

ng g c standalone --standalone

After executing this command, the generated component should appear as follows.

import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';

@Component({
  selector: 'app-standalone',
  standalone: true,
  imports: [CommonModule],
  templateUrl: './standalone.component.html',
  styleUrls: ['./standalone.component.scss'],
})
export class StandaloneComponent implements OnInit {
  constructor() {}

  ngOnInit(): void {}
}

New properties?

As you could notice, in the code above, we have two new properties in the @component decorator.

  • standalone – A boolean value that tells the Angular’s standalone API that this component is actually standalone.
  • imports – An array that accepts modules, pipes, directives, and components (The components have to be standalone as well, to be able to import them).

Using components in modules and components that are not standalone

As the standalone component is not going to be imported into any module while generating it, we have to do it manually if we are looking to import the component into a non-standalone / standard component.

ADVERTISEMENT

So, to achieve this, we are going to, in this case, app.module.ts and to add the standalone component into imports array, not into the declarations as is the case with standard components.

So our app module should look like this:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { StandaloneComponent } from './standalone/standalone.component';

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

After importing the component we are able to use it in our app.component.ts.


Importing standalone components into standalone components

This step is really easy. Let’s say that we have created a new standalone component named button and we have to import it into the standalone component that we generated earlier.

To achieve this, we would have just to add that component to our imports located in the standalone component’s decorator, as in the following example.

import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ButtonComponent } from './components/button/button.component';

@Component({
  selector: 'app-standalone',
  standalone: true,
  imports: [CommonModule, ButtonComponent],
  templateUrl: './standalone.component.html',
  styleUrls: ['./standalone.component.scss'],
})
export class StandaloneComponent implements OnInit {
  constructor() {}

  ngOnInit(): void {}
}

Now, we are able to use the button component in our standalone component and do whatever we want.


Conclusion

Standalone components are really flexible and easy to use. We can look at them now as real components, or as shared components / shared apps/libs.. etc., as we are able if structured properly, just to copy the component from one to another project and use it out of the box.

Here is also a link to a repository that I created for this purpose: https://github.com/adnan-halilovic/standalone-components-angular

You can watch the tutorial video on this topic.

ADVERTISEMENT

Thank you for reading the article.

For more content like this, follow me on youtube and other social networks that you can find on the website.

Source: YouTube
Via: Adnan Halilovic's YouTube Channel
Tags: Angularstandalone components
ADVERTISEMENT
Previous Post

Protecting Routes in React JS

Next Post

Lazyloading Standalone Components in Angular

Related Posts

VS Code Setup for Angular Developers Featured Image
Angular

Best VS Code Setup for Angular Development

October 13, 2024
My Top 5 Uncommon VS Code Extensions for 2024!
Frontend

My Top 5 Uncommon VS Code Extensions for 2024!

January 27, 2024
Mastering DevTools: Supercharge Your Workflow with Code Snippets!
Frontend

Mastering DevTools: Supercharge Your Workflow with Code Snippets!

September 24, 2023
Quokka.js and Wallaby.js Giveaway
Frontend

Quokka.js and Wallaby.js Giveaway

July 26, 2023
How to Work on Different Git Branches at the Same Time?
Git

How to Work on Different Git Branches at the Same Time?

December 4, 2022
Lazyload Standalone Components in Angular
Angular

Lazyloading Standalone Components in Angular

September 11, 2022
Next Post
Lazyload Standalone Components in Angular

Lazyloading Standalone Components in Angular

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Latest posts

The One MCP Server Every Developer Should Be Using
AI Tips and Tricks

The One MCP Server Every Developer Should Be Using

by Adnan Halilovic
May 13, 2025
0

What Is Context 7 MCP Server? Context 7 is a Model Context Provider (MCP) server that delivers real-time documentation and metadata to...

Read moreDetails
Boosting Productivity with AI-Powered IDEs: My Workflow with Windinsurf

Boosting Productivity with AI-Powered IDEs: My Workflow with Windinsurf

May 12, 2025
Why I Switched from VS Code to Windsurf — An AI Code Editor That Changed My Workflow

Why I Switched from VS Code to Windsurf — An AI Code Editor That Changed My Workflow

May 12, 2025
The Best Coding Font to Boost Your Productivity!

The Best Coding Font to Boost Your Productivity!

October 17, 2024
VS Code Setup for Angular Developers Featured Image

Best VS Code Setup for Angular Development

October 13, 2024
Github LinkedIn Youtube Twitter Pinterest Instagram Reddit

About Me

Adnan Halilovic Blog

Adnan Halilović

Software Developer - Content Writer

I am Adnan Halilović, the man behind the website and channels that you are currently reading and viewing!

I am a software developer with over 15 years of expertise in a variety of fields.

Newsletter

Country:

Email address:


Recent from Instagram

    The Instagram Access Token is expired, Go to the Customizer > JNews : Social, Like & View > Instagram Feed Setting, to refresh it.

© 2022 Adnan Halilovic - Software development content, tips & tricks.

Click to Copy
No Result
View All Result
  • Technologies
  • AI Tips and Tricks
  • Frontend
  • Backend
  • Server
  • Contact
  • About

© 2022 Adnan Halilovic - Software development content, tips & tricks.