Angular Standalone Components

For a while the Angular community had been asking for standalone components i.e components that are independent and do not need a module(NgModule). With Angular version 14 that capability was added. There are those who still want NgModules and don’t see the need for standalone components and because of this the feature was made optional so as to accommodate everyone.

In this article we’ll look at how to create and integrate standalone components in our Angular application. There are different scenarios that could happen and they are

  • Only 1 component in the application which is a standalone component.
  • Adding a standalone component to another standalone component.
  • Adding a module to a stand alone component.

  1. Only 1 component in the application which is a standalone component.

Lets say we have a component called AppComponent that has one property called title and displays that title in the template.

Lets generate the project. We’ll call the project single-standalone

`ng new single-standalone`

`cd single-standalone`

`ng serve`

Lets delete app.module.ts. We don’t need it since its a standalone component

In the app.component.ts file lets add the property ‘standalone’ in the @Component decorator and set it to true

app.component.ts

You should get an error like below

The above error is because main.ts file is still trying to load the module we deleted.

Lets make a few changes to main.ts file

Delete the AppModule import statement and the platformBrowserDynamic import.

Add a new import for bootstrapApplication and the app.component.ts file

main.ts
app.component.ts
app.component.html

Everything should compile properly now. Your page should look as below

http://localhost:4200/

And that’s its. It wasn’t hard was it?

Now onto the second one

2. Adding a standalone component to another standalone component.

Since we already have our initial standalone component i.e AppComponent. We’ll create another standalone component and import it to our AppCompoment. Our AppComponent will remain as the root component.

For our new component, lets call it SecondStandAloneComponent

`ng g c SecondStandAlone –skip-import`

`ng serve`

we use the –skip-import option to avoid importing to any module

Next we’ll edit the SecondStandAloneComponent and add the standalone property and set it to true. We’ll also add a title property to display to the template.

second-stand-alone-components.ts

Now lets import it to our root component so we can use it.

app-components.ts

Next we’ll add it to our root template.

app-component.html

Your web page should look as shown below

http://localhost:4200/

3.Adding a module to a stand alone component.

Using the same project now lets add a new module and component. Lets call our module MyTestModule

my-test-module.ts

Next we’ll create a TestComponent and add it to the MyTestModule

test-component.ts

It should already have been added to MyTestModule since we added it while generating the component

my-test-module.ts

Lets add a title property to TestComponent and render it in its corresponding template

test-component.ts
test-component.html

Now lets import it in our AppComponent. Since TestComponent is part of MyTestModule, we will import MyTestModule.

app-component.ts

Now for the template

app-component.html

Now lets check in the browser

http://localhost:4200/

Nice!

The Angular team also released standalone directives as well as pipes in Angular v14. They also showed how to inject services in the stand alone components. You can have a look at the official docs

Leave a Reply

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