Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Transition to Standalone Components | Standalone Components & Modules
Introduction to Angular
course content

Kursinnehåll

Introduction to Angular

Introduction to Angular

1. Angular Fundamentals
2. Components and Templates
3. Mastering Angular Directives and Pipes
4. Services and Dependency Injection in Angular
5. Standalone Components & Modules
6. Routing and Navigation in Angular

book
Transition to Standalone Components

In earlier versions of Angular, every component had to be part of a module. Even the simplest component couldn't exist on its own — it had to be declared inside an NgModule.

This was like needing a whole kitchen just to make a cup of tea. Sounds bulky, right?

That's why, over time, the Angular team started moving toward simplifying the structure. This led to the introduction of a new approach — Standalone Components.

What is a Standalone Component?

Note
Definition

A Standalone Component is a component that doesn't require declaration inside a module (NgModule). It's self-contained, meaning it holds all the information about its dependencies within itself.

To create one, you simply add the standalone: true flag inside the @Component decorator and specify any required dependencies using the imports array:

ts

example

copy
1234567
@Component({ standalone: true, selector: 'app-example', imports: [CommonModule], // everything the component needs to work templateUrl: './example.component.html' }) export class ExampleComponent {}

Here, adding standalone: true is a special option inside the @Component decorator that tells Angular this component is independent — it doesn't need to be declared in an NgModule.

Also, we specify a list of external dependencies (imports) that the component requires. In the traditional module-based approach, these dependencies were passed to the module's imports. But when components work without a module, their dependencies need to be declared directly inside the component — and Angular acts as a module internally for that component.

Note
Note

We built our whole project using standalone components, even if we didn't explicitly add standalone: true, because we didn't use NgModules at all and worked only with structures that Angular treats as standalone.

Why is Angular moving away from NgModules?

NgModules played an important role in large applications:

  • They helped organize the code;

  • They gave control over the scope (visibility);

  • They were useful for optimization.

But over time, it became clear that many of these things can be done at the component level — more simply and intuitively.

Note
Note

Angular hasn't removed modulesstandalone components just offer a more flexible option.

In other words, standalone components are the modern way to build components without modules. They simplify the structure, make components self-sufficient, and easier to use.

Angular continues to support modules but recommends using standalone components for new code.

1. What does standalone: true do in a component decorator?

2. Why is the imports property needed in a Standalone Component?

question mark

What does standalone: true do in a component decorator?

Select the correct answer

question mark

Why is the imports property needed in a Standalone Component?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 3

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

course content

Kursinnehåll

Introduction to Angular

Introduction to Angular

1. Angular Fundamentals
2. Components and Templates
3. Mastering Angular Directives and Pipes
4. Services and Dependency Injection in Angular
5. Standalone Components & Modules
6. Routing and Navigation in Angular

book
Transition to Standalone Components

In earlier versions of Angular, every component had to be part of a module. Even the simplest component couldn't exist on its own — it had to be declared inside an NgModule.

This was like needing a whole kitchen just to make a cup of tea. Sounds bulky, right?

That's why, over time, the Angular team started moving toward simplifying the structure. This led to the introduction of a new approach — Standalone Components.

What is a Standalone Component?

Note
Definition

A Standalone Component is a component that doesn't require declaration inside a module (NgModule). It's self-contained, meaning it holds all the information about its dependencies within itself.

To create one, you simply add the standalone: true flag inside the @Component decorator and specify any required dependencies using the imports array:

ts

example

copy
1234567
@Component({ standalone: true, selector: 'app-example', imports: [CommonModule], // everything the component needs to work templateUrl: './example.component.html' }) export class ExampleComponent {}

Here, adding standalone: true is a special option inside the @Component decorator that tells Angular this component is independent — it doesn't need to be declared in an NgModule.

Also, we specify a list of external dependencies (imports) that the component requires. In the traditional module-based approach, these dependencies were passed to the module's imports. But when components work without a module, their dependencies need to be declared directly inside the component — and Angular acts as a module internally for that component.

Note
Note

We built our whole project using standalone components, even if we didn't explicitly add standalone: true, because we didn't use NgModules at all and worked only with structures that Angular treats as standalone.

Why is Angular moving away from NgModules?

NgModules played an important role in large applications:

  • They helped organize the code;

  • They gave control over the scope (visibility);

  • They were useful for optimization.

But over time, it became clear that many of these things can be done at the component level — more simply and intuitively.

Note
Note

Angular hasn't removed modulesstandalone components just offer a more flexible option.

In other words, standalone components are the modern way to build components without modules. They simplify the structure, make components self-sufficient, and easier to use.

Angular continues to support modules but recommends using standalone components for new code.

1. What does standalone: true do in a component decorator?

2. Why is the imports property needed in a Standalone Component?

question mark

What does standalone: true do in a component decorator?

Select the correct answer

question mark

Why is the imports property needed in a Standalone Component?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 3
Vi beklagar att något gick fel. Vad hände?
some-alt