Marcin Jahn | Dev Notebook
  • Home
  • Programming
  • Technologies
  • Projects
  • About
  • Home
  • Programming
  • Technologies
  • Projects
  • About
  • An icon of the Core section Core
    • Programs Execution
    • Stack and Heap
    • Asynchronous Programming
      • Overview
      • Event Queues
      • Fibers
      • Stackless Coroutines
  • An icon of the .NET section .NET
    • HTTPClient
    • Async
      • How Async Works
      • TAP Tips
    • Equality
    • Comparisons
    • Enumerables
    • Unit Tests
    • Generic Host
    • Logging
    • Configuration
    • Records
    • Nullability
    • Garbage Collector
    • IL and Allocations
    • gRPC
    • Source Generators
    • Platform Invoke
    • ASP.NET Core
      • Overview
      • Middleware
      • Razor Pages
      • Routing in Razor Pages
      • Web APIs
      • Filters
      • Identity
      • Validation
      • Tips
    • Entity Framework Core
      • Overview
      • Testing
      • Tips
  • An icon of the Angular section Angular
    • Overview
    • Components
    • Directives
    • Services and DI
    • Routing
    • Observables (RxJS)
    • Forms
    • Pipes
    • HTTP
    • Modules
    • NgRx
    • Angular Universal
    • Tips
    • Standalone Components
  • An icon of the JavaScript section JavaScript
    • OOP
    • JavaScript - The Weird Parts
    • JS Functions
    • ES Modules
    • Node.js
    • Axios Tips
    • TypeScript
      • TypeScript Environment Setup
      • TypeScript Tips
    • React
      • React Routing
      • MobX
    • Advanced Vue.js Features
  • An icon of the Rust section Rust
    • Overview
    • Cargo
    • Basics
    • Ownership
    • Structures
    • Enums
    • Organization
    • Collections
    • Error Handling
    • Generics
    • Traits
    • Lifetimes
    • Closures
    • Raw Pointers
    • Smart Pointers
    • Concurrency
    • Testing
    • Tips
  • An icon of the C/C++ section C/C++
    • Compilation
    • Structures
    • OOP in C
    • Pointers
    • Strings
    • Dynamic Memory
    • argc and argv Visualization
  • An icon of the GTK section GTK
    • Overview
    • GObject
    • GJS
  • An icon of the CSS section CSS
    • Responsive Design
    • CSS Tips
    • CSS Pixel
  • An icon of the Unity section Unity
    • Unity
  • An icon of the Functional Programming section Functional Programming
    • Fundamentals of Functional Programming
    • .NET Functional Features
    • Signatures
    • Function Composition
    • Error Handling
    • Partial Application
    • Modularity
    • Category Theory
      • Overview
      • Monoid
      • Other Magmas
      • Functors
  • An icon of the Algorithms section Algorithms
    • Big O Notation
    • Array
    • Linked List
    • Queue
    • Hash Table and Set
    • Tree
    • Sorting
    • Searching
  • An icon of the Architecture section Architecture
    • What is architecture?
    • Domain-Driven Design
    • ASP.NET Core Projects
  • An icon of the Core section Core
    • Programs Execution
    • Stack and Heap
    • Asynchronous Programming
      • Overview
      • Event Queues
      • Fibers
      • Stackless Coroutines
  • An icon of the .NET section .NET
    • HTTPClient
    • Async
      • How Async Works
      • TAP Tips
    • Equality
    • Comparisons
    • Enumerables
    • Unit Tests
    • Generic Host
    • Logging
    • Configuration
    • Records
    • Nullability
    • Garbage Collector
    • IL and Allocations
    • gRPC
    • Source Generators
    • Platform Invoke
    • ASP.NET Core
      • Overview
      • Middleware
      • Razor Pages
      • Routing in Razor Pages
      • Web APIs
      • Filters
      • Identity
      • Validation
      • Tips
    • Entity Framework Core
      • Overview
      • Testing
      • Tips
  • An icon of the Angular section Angular
    • Overview
    • Components
    • Directives
    • Services and DI
    • Routing
    • Observables (RxJS)
    • Forms
    • Pipes
    • HTTP
    • Modules
    • NgRx
    • Angular Universal
    • Tips
    • Standalone Components
  • An icon of the JavaScript section JavaScript
    • OOP
    • JavaScript - The Weird Parts
    • JS Functions
    • ES Modules
    • Node.js
    • Axios Tips
    • TypeScript
      • TypeScript Environment Setup
      • TypeScript Tips
    • React
      • React Routing
      • MobX
    • Advanced Vue.js Features
  • An icon of the Rust section Rust
    • Overview
    • Cargo
    • Basics
    • Ownership
    • Structures
    • Enums
    • Organization
    • Collections
    • Error Handling
    • Generics
    • Traits
    • Lifetimes
    • Closures
    • Raw Pointers
    • Smart Pointers
    • Concurrency
    • Testing
    • Tips
  • An icon of the C/C++ section C/C++
    • Compilation
    • Structures
    • OOP in C
    • Pointers
    • Strings
    • Dynamic Memory
    • argc and argv Visualization
  • An icon of the GTK section GTK
    • Overview
    • GObject
    • GJS
  • An icon of the CSS section CSS
    • Responsive Design
    • CSS Tips
    • CSS Pixel
  • An icon of the Unity section Unity
    • Unity
  • An icon of the Functional Programming section Functional Programming
    • Fundamentals of Functional Programming
    • .NET Functional Features
    • Signatures
    • Function Composition
    • Error Handling
    • Partial Application
    • Modularity
    • Category Theory
      • Overview
      • Monoid
      • Other Magmas
      • Functors
  • An icon of the Algorithms section Algorithms
    • Big O Notation
    • Array
    • Linked List
    • Queue
    • Hash Table and Set
    • Tree
    • Sorting
    • Searching
  • An icon of the Architecture section Architecture
    • What is architecture?
    • Domain-Driven Design
    • ASP.NET Core Projects

Standalone Components in Angular

Preview

Standalone Components is a stable feature since Angular 15.

Standalone Components is a feature that deals with the boiletplate code that we have to currently write in a typical Angular app. Namely, it enables us to create components without dealing with the modules and all their declarations, imports, and so on. We can actually get rid of modules entirely, even the AppModule! That makes Angular a bit more similar to React or Vue.js, which do not use the module system and are much leaner to work with.

Not Just Components

Standalone Components is not only about components. It’s also Standalone …

  • … Pipes
  • … Directives

Standalone components can be mixed with the “classic” NgModule style.

Example

@Component({
standalone: true,
selector: 'app-my-component',
templateUrl: './my-component.component.html'
})
export class MyComponent {}

Standalone components can’t be declared in any module. standalone: true makes it recognizable globally.

There are two ways to use standalone components in other components:

  • new, when the parent component is also standalone
  • “legacy”, with NgModule

Standalone Parent

@Component({
standalone: true,
imports: [ MyComponent ],
selector: 'app-my-other-component',
templateUrl: './my-other-component.component.html'
})
export class MyOtherComponent {}

This way is very similar to how we’d do it in Vue.js.

Legacy Parent

Since standalone components can be mixed with NgModule-declared components, there’s a way to use standalone components in such “legacy” components.

First, we need to import the standalone component in our parent component’s module:

@NgModule({
declarations: [
MyOtherComponent
],
imports: [
BrowserModule
MyComponent
]
})
export class AppModule {}

Previously, we’d have to put MyComponent in the declarations instead of imports (assuming we had just one module in our app).

Now, the legacy component can make use of MyComponent in its template, as it typically would in “legacy” NgModule-based world.

Importing Modules

The standalone components may not only import other standalone components, but also whole modules! It is useful when our app uses some “legacy” entities that are exported by some module(s). To use these entities, our component has toimport the module.

If standalone component is the only thing that uses the imported module, that module does not need to be imported in any other module, as it would typiclaly be the case.

Standalone Root Component

We can get rid of modules entirely, including the main AppModule. Without that module though, we need a different way of bootstrapping our app with the AppComponent:

bootstrapApplication(AppComponent);

As a reminder, here’s the “legacy” way:

platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));

Dependency Injection

With modules being gone, we need another way of providing services globally. Well there is the @Injectable constructor, but it’s not everyone’s favorite to provide service from the service itself.

We can use the new bootstrapApplication and it’s second parameter:

bootstrapApplication(AppComponent,
{
providers: [ MyService ]
});

MyService will be available globally.

Routing and Lazy Loading

Lazy loading with standalone components is doable, it requires us to use a new loadComponent function in our routes to load the component dynamically.

The previously used loadChildren function is still useful. Instead of loading a module, we can use it to load a file containing child routes.

←  Tips
© 2023 Marcin Jahn | Dev Notebook | All Rights Reserved. | Built with Astro.