<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Henrique Custódia</title>
    <description>The latest articles on DEV Community by Henrique Custódia (@henriquecustodia).</description>
    <link>https://dev.to/henriquecustodia</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F30163%2F8cc08956-2e0c-4db0-acb9-88fcb0b2e326.jpg</url>
      <title>DEV Community: Henrique Custódia</title>
      <link>https://dev.to/henriquecustodia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/henriquecustodia"/>
    <language>en</language>
    <item>
      <title>Creating Custom RxJS Operators</title>
      <dc:creator>Henrique Custódia</dc:creator>
      <pubDate>Sun, 26 Feb 2023 18:50:04 +0000</pubDate>
      <link>https://dev.to/henriquecustodia/creating-custom-rxjs-operators-2o0</link>
      <guid>https://dev.to/henriquecustodia/creating-custom-rxjs-operators-2o0</guid>
      <description>&lt;p&gt;Since &lt;a href="https://angular.io/" rel="noopener noreferrer"&gt;Angular&lt;/a&gt; has become a popular framework, RxJS is becoming increasingly popular. In this post I would like to write about a very nice feature in this fantastic library: &lt;strong&gt;custom operators&lt;/strong&gt;!&lt;/p&gt;

&lt;p&gt;A few months ago I have been trying to keep the chaining logic more readable in my RxJS code. It's very easy to write confusing code when we don't pay attention to how the operators are being composed.&lt;/p&gt;

&lt;p&gt;In this post, I would like to show how to write an RxJS code using custom operators to keep the logic readable and easier to maintain over time.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to create a custom operator? 🤔
&lt;/h2&gt;

&lt;p&gt;It's very easy, really!&lt;/p&gt;

&lt;p&gt;An RxJS operator is just a function that receives an &lt;a href="https://rxjs.dev/guide/observable" rel="noopener noreferrer"&gt;observable&lt;/a&gt; as a parameter and returns a new &lt;a href="https://rxjs.dev/guide/observable" rel="noopener noreferrer"&gt;observable&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For instance:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fcreating-custom-rxjs-operators-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fcreating-custom-rxjs-operators-1.png" alt="custom-rxjs-operators-1" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Generally, it's common to create factory operators (operators that return another function), because it'll become more intuitive for the developers to understand the operators' chain.&lt;/p&gt;

&lt;p&gt;Here's an example of a factory operator:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fcreating-custom-rxjs-operators-2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fcreating-custom-rxjs-operators-2.png" alt="custom-rxjs-operators-2" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's see some examples of how to use custom operators to create more readable code!&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating an operator to filter odd numbers 🦄
&lt;/h2&gt;

&lt;p&gt;It's simple to filter odd numbers, isn't it?&lt;/p&gt;

&lt;p&gt;But creating a custom operator to name it'll become your code cleaner and more obvious. The readability will improve.&lt;/p&gt;

&lt;p&gt;Let's see the code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fcreating-custom-rxjs-operators-3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fcreating-custom-rxjs-operators-3.png" alt="custom-rxjs-operators-3" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating an operator to search by text 🕵🏾‍♂️
&lt;/h2&gt;

&lt;p&gt;Searching by text into an array might be easy, but generally doing it with RxJS turns the code a bit hard to understand over time.&lt;/p&gt;

&lt;p&gt;But, before using a custom operator, let's see how the code would look when not using custom operators:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fcreating-custom-rxjs-operators-5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fcreating-custom-rxjs-operators-5.png" alt="custom-rxjs-operators-5" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's not impossible to understand the code logic, but it's difficult to get what it's doing when taking a quick look.&lt;/p&gt;

&lt;p&gt;Because of that, naming the code logic is important. &lt;/p&gt;

&lt;p&gt;That said, let's refactor the code above using custom operators: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fcreating-custom-rxjs-operators-6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fcreating-custom-rxjs-operators-6.png" alt="custom-rxjs-operators-6" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;toLowerCase&lt;/code&gt; and &lt;code&gt;searchByText&lt;/code&gt; are operators that can be used to reuse the code, and it's much easier to understand what they're doing. &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion  🥷
&lt;/h2&gt;

&lt;p&gt;Using custom operators can help us to make the code more readable and easier to reuse across the projects. &lt;/p&gt;

&lt;p&gt;It was a quick post about custom operators. That feature is helping me a lot to enforce the reusability in my Angular projects. &lt;/p&gt;

&lt;p&gt;Thank you for reading!  ✨&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Angular 15: Using The Directive Composition API</title>
      <dc:creator>Henrique Custódia</dc:creator>
      <pubDate>Wed, 02 Nov 2022 04:21:13 +0000</pubDate>
      <link>https://dev.to/henriquecustodia/angular-15-using-the-directive-composition-api-58j3</link>
      <guid>https://dev.to/henriquecustodia/angular-15-using-the-directive-composition-api-58j3</guid>
      <description>&lt;p&gt;Angular v15 will be released pretty soon, and it's coming with a very nice feature called &lt;strong&gt;Directive Composition API&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  But, what is it? 🤔
&lt;/h3&gt;

&lt;p&gt;The Directive Composition API allows us to compose directives into components and other directives.&lt;/p&gt;

&lt;p&gt;This API works only with standalone components (and standalone directives). As the 14 version added the standalone property, the 15 version added a &lt;strong&gt;hostDirectives&lt;/strong&gt; property.&lt;/p&gt;

&lt;p&gt;Let's see how to use this property.&lt;/p&gt;

&lt;p&gt;The example below shows a directive called &lt;strong&gt;BoxDirective&lt;/strong&gt;, that sets some styles to the &lt;strong&gt;AppComponent&lt;/strong&gt; using the &lt;strong&gt;Composition API&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-1.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result will be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fresult1.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fresult1.PNG" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can use this new API to reuse a lot of code. It's amazing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Exposing the directive's input
&lt;/h3&gt;

&lt;p&gt;When adding Inputs and Outputs to a directive, we need to expose those to the component to be able to use them. There are the &lt;strong&gt;input&lt;/strong&gt; and &lt;strong&gt;output&lt;/strong&gt; in the &lt;strong&gt;hostDirectives&lt;/strong&gt; property because of it.&lt;/p&gt;

&lt;p&gt;The following example adds an Input property to the BoxDirective.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-2.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And we'll add the &lt;strong&gt;color input&lt;/strong&gt; to the inputs array to expose it to the component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-3.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That way we can set a color to the &lt;strong&gt;BoxDirective&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-4.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result will be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fresult2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fresult2.PNG" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's possible to rename the exposed input's name - generally useful when we have directives' inputs with the same name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-5.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using the renamed property:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-6.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Exposing the directive's output
&lt;/h3&gt;

&lt;p&gt;To expose outputs as easily as with inputs. But there's a property called &lt;strong&gt;outputs&lt;/strong&gt; specifically for it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-7.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-8.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using it&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-9.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the same way as inputs, it's possible to rename the output's name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-10.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-10.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-11.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-11.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus: OnDestroyDirective 💎
&lt;/h3&gt;

&lt;p&gt;Let's create a reusable directive to destroy old subscriptions.&lt;/p&gt;

&lt;p&gt;The following code creates a timer component that uses an interval operator to log an incremental number every second. To avoid memory leaks, it's a good practice to remove observable subscriptions when a component has been destroyed. The &lt;strong&gt;OnDestroyDirective&lt;/strong&gt; will remove the interval subscription automatically after the timer component is destroyed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-12.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-12.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-13.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fraycast-untitled-13.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result will be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2F2022-11-01-23-32-47.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2F2022-11-01-23-32-47.gif" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  👨‍💻
&lt;/h3&gt;

&lt;p&gt;You can check out the code of this post &lt;a href="https://stackblitz.com/edit/ng-15-directive-composition-api?file=README.md" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  That's all!
&lt;/h3&gt;

&lt;p&gt;I've spent some time writing this post, then, I hope that you enjoyed it!&lt;/p&gt;

&lt;p&gt;If you liked it, give some claps/likes to this post or share it with your friends!&lt;/p&gt;

&lt;p&gt;Thank you for the reading. 😄&lt;/p&gt;

&lt;p&gt;See you! 👋🏼&lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Angular Inject Function: A New Way To Work With DI</title>
      <dc:creator>Henrique Custódia</dc:creator>
      <pubDate>Thu, 13 Oct 2022 20:42:53 +0000</pubDate>
      <link>https://dev.to/henriquecustodia/angular-inject-function-a-new-way-to-work-with-di-2j47</link>
      <guid>https://dev.to/henriquecustodia/angular-inject-function-a-new-way-to-work-with-di-2j47</guid>
      <description>&lt;p&gt;It's time to talk about one of the best features in Angular 14: &lt;strong&gt;inject function.&lt;/strong&gt;  I've been studying this new version for a while and I can say... Angular is getting better and better. Since the improvements in performance up to the developer's usability, it's becoming a great framework to work with.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I've written a post about standalone components, it's an amazing Angular 14 feature as well. &lt;a href="https://www.henriquecustodia.dev/posts/angular-standalone-components:-say-goodbye-to-ngmodules/" rel="noopener noreferrer"&gt;Check out here&lt;/a&gt;!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But, well, let's talk about the &lt;strong&gt;inject function.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What is it?
&lt;/h3&gt;

&lt;p&gt;It's a new feature released in Angular 14 that allows us to inject providers (services, tokens, components, etc.) without using the constructor class. It can be handy for those moments we aren't using a class - like when creating a factory provider for instance.&lt;/p&gt;

&lt;p&gt;Also, we can use this function to avoid injecting providers using the well-known class constructor. It can be good for a more cleaned approach - avoiding several injects in the constructors which can be confusing along the time.&lt;/p&gt;

&lt;p&gt;The following example shows how to inject a service and use it in the component:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fapp-component0-ts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fapp-component0-ts.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's very simple to use, indeed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Inject dependencies into functions 👻
&lt;/h3&gt;

&lt;p&gt;I think the better part about this inject function is the possibility to create "smart functions" that can inject services doing the code easier to understand.&lt;/p&gt;

&lt;p&gt;Let's see an example of a function that injects the &lt;strong&gt;Renderer&lt;/strong&gt; service and changes the CSS class of an HTML element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Ffunctions-ts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Ffunctions-ts.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fapp-component1-ts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fapp-component1-ts.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have you noticed?&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;createClassManager&lt;/strong&gt; and &lt;strong&gt;getHost&lt;/strong&gt; are using the inject function. That's a good way to abstract the code into functions. Before, we had services to help us to break into pieces our code. But now, we can use functions that are a very natural way to work in Javascript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where can we use this function?
&lt;/h3&gt;

&lt;p&gt;Based on angular documentation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In practice the &lt;code&gt;inject()&lt;/code&gt; calls are allowed in a constructor, a constructor parameter and a field initializer.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The inject function just works inside the injection context. We can call it when the instance is being created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fapp-component2-ts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fapp-component2-ts.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Ffactory-provider-ts-3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Ffactory-provider-ts-3.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Injection Flags 🚩
&lt;/h3&gt;

&lt;p&gt;Another nice thing about the inject function is the new way we can use the injection flags. Before, injecting the provider by constructor class, we had to use decorators like &lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/host"&gt;@host&lt;/a&gt;&lt;/strong&gt; or &lt;strong&gt;@Optional&lt;/strong&gt; to change an injection behavior. Now, with inject function, we just need to set some boolean flags using an options object as the second parameter.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Finject-function-ts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Finject-function-ts.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;it's much more simple, isn't it?&lt;/p&gt;

&lt;p&gt;Using the injection by constructor class we'd have to use the way:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fconstructor-ts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fconstructor-ts.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In my opinion, this new approach is much more simple to understand. Mainly, for those who are just starting to work with Angular.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should we stop to use class constructors with DI?
&lt;/h3&gt;

&lt;p&gt;Well, I don't think so.&lt;/p&gt;

&lt;p&gt;Although the inject function is a great feature, it's just a new approach. We don't need to refactor the projects because of it. Probably class constructors and inject functions will be two options that'll coexist for a long time.&lt;/p&gt;

&lt;p&gt;But, in my opinion, inject function brings a much more clean approach to work with DI. It's straightforward and avoids lots of parameters on the constructor.&lt;/p&gt;

&lt;h3&gt;
  
  
  👨‍💻
&lt;/h3&gt;

&lt;p&gt;You can check out the code of this post on my &lt;a href="https://github.com/henriquecustodia/angular-inject-function-example" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  That's all!
&lt;/h3&gt;

&lt;p&gt;I've spent some time writing this post, then, I hope that you enjoyed it!&lt;/p&gt;

&lt;p&gt;If you liked it, give some claps/likes to this post or &lt;a href="https://www.buymeacoffee.com/henricustodia" rel="noopener noreferrer"&gt;support me in buying a coffee&lt;/a&gt;; it'll help me a lot! 👏🏼❤&lt;/p&gt;

&lt;p&gt;Thank you for the reading. 😄&lt;/p&gt;

&lt;p&gt;See you! 👋🏼&lt;/p&gt;

</description>
      <category>angular</category>
      <category>typescript</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Angular Standalone Components: Say Goodbye To NgModules</title>
      <dc:creator>Henrique Custódia</dc:creator>
      <pubDate>Sun, 25 Sep 2022 20:14:10 +0000</pubDate>
      <link>https://dev.to/henriquecustodia/angular-standalone-components-say-goodbye-to-ngmodules-1p45</link>
      <guid>https://dev.to/henriquecustodia/angular-standalone-components-say-goodbye-to-ngmodules-1p45</guid>
      <description>&lt;p&gt;A few months ago, Angular v14 was released with a lot of amazing new features. And, since then, I've spent some time studying and applying these to my projects.&lt;/p&gt;

&lt;p&gt;It's hard not to admit how excited I'm feeling about the standalone component feature. This feature, in my opinion, is the most relevant improvement that Angular has had over the years. That will change how we develop our applications, and, mainly, that will make the components simpler to read and write. In the future, we probably won't need NgModules anymore.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's a standalone component?
&lt;/h3&gt;

&lt;p&gt;Well, the standalone component is just a common Angular component that we already know but with further features. That is a combination of a component and a NgModule.&lt;/p&gt;

&lt;p&gt;Therefore, we don't need to import our component into a NgModule to use it, because this kind of component is self-contained.&lt;/p&gt;

&lt;p&gt;Let's see a simple code to understand how to use it.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-header&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;standalone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;CommonModule&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;templateUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./header.component.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styleUrls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./header.component.scss&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;changeDetection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ChangeDetectionStrategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;OnPush&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The code above has two new properties called &lt;code&gt;standalone&lt;/code&gt; and &lt;code&gt;imports&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But, what's it means?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;standalone:&lt;/strong&gt; It marks the component as a standalone component and enables all features related to it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;imports:&lt;/strong&gt; It allows a component to import Directives, Pipes, Other Components, and existing NgModules.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Oh, can we import a component directly into another component?
&lt;/h3&gt;

&lt;p&gt;Yes, we can! We don't need to create a NgModule just to register components anymore. With standalone components, we can import a component into another component, and &lt;strong&gt;Aha!&lt;/strong&gt; it just works.&lt;/p&gt;

&lt;p&gt;Let's see a quick example of it.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// Let's create header component&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-header&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;standalone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`
    &amp;lt;p&amp;gt;header works!&amp;lt;/p&amp;gt;
  `&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-layout&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;standalone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;HeaderComponent&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;app-header&amp;gt;&amp;lt;/app-header&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The result will be:&lt;/p&gt;

&lt;p&gt;&lt;a href="/images/uploads/app-root-template-rendered.PNG" class="article-body-image-wrapper"&gt;&lt;img src="/images/uploads/app-root-template-rendered.PNG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In another way using NgModule we'd have to create the following code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-header&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`
    &amp;lt;p&amp;gt;header works!&amp;lt;/p&amp;gt;
  `&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-layout&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;lt;app-header&amp;gt;&amp;lt;/app-header&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;NgModule&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;declarations&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;HeaderComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="nx"&gt;LayoutComponent&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It's much more verbose, isn't it?&lt;/p&gt;

&lt;p&gt;The new approach with standalone components allows us to make Angular components simpler and more straightforward.&lt;/p&gt;

&lt;h3&gt;
  
  
  And, how to bootstrap an app using only standalone components?
&lt;/h3&gt;

&lt;p&gt;With this new approach, we have a new function called &lt;code&gt;bootstrapApplication&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This function allows Angular to bootstrap an application using a standalone component directly, no more a NgModule as we've known.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="nf"&gt;bootstrapApplication&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppComponent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Generally, this code stands in &lt;code&gt;main.ts&lt;/code&gt; file&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With this approach, we need to change how we set the providers in the application as well. Let's see this in the next topic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting providers to the application
&lt;/h3&gt;

&lt;p&gt;Yep, a lot of things have changed! 😁&lt;/p&gt;

&lt;p&gt;The  &lt;code&gt;bootstrapApplication&lt;/code&gt; function has a second parameter that allows us to add some configuration to the application.&lt;/p&gt;

&lt;p&gt;Let's see the typescript declaration of it.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;declare&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;ApplicationConfig&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;ImportedNgModuleProviders&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As we can see, the &lt;code&gt;providers&lt;/code&gt; property supports the &lt;code&gt;Provider&lt;/code&gt; and &lt;code&gt;ImportedNgModuleProviders&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Well, let's understand how to register a simple provider in the application - using the &lt;code&gt;Provider&lt;/code&gt; type.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ENABLE_DARK_THEME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InjectionToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ENABLE_DARK_THEME&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;ImportedNgModuleProviders&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ENABLE_DARK_THEME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;useValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nf"&gt;bootstrapApplication&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;LayoutComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;providers&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And to use the registered provider is the same way as we've already known.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LayoutComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ENABLE_DARK_THEME&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;enableDarkTheme&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Nothing that new so far, just the way we register the providers have changed.&lt;/p&gt;

&lt;h3&gt;
  
  
  What about routes, how can we register the app routes?
&lt;/h3&gt;

&lt;p&gt;I was waiting for this question! It's changed as well. 😄&lt;/p&gt;

&lt;p&gt;To register the app routes we have to use the new function called&lt;code&gt;importProvidersFrom&lt;/code&gt;. Let's see what &lt;a href="https://angular.io/api/core/importProvidersFrom" rel="noopener noreferrer"&gt;Angular documentation&lt;/a&gt; says about it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Collects providers from all NgModules and standalone components, including transitively imported.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This function will extract all providers of the NgModule and set them to the provider's app array.&lt;/p&gt;

&lt;p&gt;Let's see how to configure the app routes:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ROUTES&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;loadComponent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
            &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app/pages/home-page/home-page.component&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;HomePageComponent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;ImportedNgModuleProviders&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;importProvidersFrom&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
        &lt;span class="nx"&gt;RouterModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ROUTES&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nf"&gt;bootstrapApplication&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;LayoutComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;providers&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-layout&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;standalone&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;HeaderComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;RouterModule&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`
    &amp;lt;app-header&amp;gt;&amp;lt;/app-header&amp;gt;
    &amp;lt;router-outlet&amp;gt;&amp;lt;/router-outlet&amp;gt; // Here comes the loaded component from the router.
  `&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LayoutComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It's become easier to configure the app router as we can load standalone components directly using the new &lt;code&gt;loadComponent&lt;/code&gt; method. We don't need to create a lazy NgModule that uses the &lt;code&gt;RouterModule.forChild&lt;/code&gt; anymore.&lt;/p&gt;

&lt;h3&gt;
  
  
  💭💭
&lt;/h3&gt;

&lt;p&gt;I think the Angular v14 is just the beginning of something bigger and better! Angular has changed a lot over the last few years and it's exciting.&lt;/p&gt;

&lt;p&gt;Standalone components allow us to create simpler applications in a faster way. This new feature also can make Angular an easier framework to learn, as we won't, in the future,  need to use NgModule anymore - I hope so 😆.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For more information about standalone components and all their features, check out the &lt;a href="https://angular.io/guide/standalone-components" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  👨‍💻
&lt;/h3&gt;

&lt;p&gt;You can check out the code of this post on my &lt;a href="https://github.com/henriquecustodia/standalone-components-example" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  That's all!
&lt;/h3&gt;

&lt;p&gt;I've spent some time writing this post, then, I hope that you enjoyed it!&lt;/p&gt;

&lt;p&gt;If you liked it, give it some claps/likes to this post; it'll help me a lot! 👏🏼❤&lt;/p&gt;

&lt;p&gt;Thank you for the reading. 😄&lt;/p&gt;

&lt;p&gt;See you! 👋🏼&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.buymeacoffee.com/henricustodia" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F94k4yrpf58dlsm4q77h6.png" alt="" width="170" height="37"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Creating a Micro Frontend App with Angular Module Federation</title>
      <dc:creator>Henrique Custódia</dc:creator>
      <pubDate>Tue, 30 Aug 2022 14:17:05 +0000</pubDate>
      <link>https://dev.to/henriquecustodia/creating-a-micro-frontend-app-with-angular-module-federation-ah9</link>
      <guid>https://dev.to/henriquecustodia/creating-a-micro-frontend-app-with-angular-module-federation-ah9</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This post was inspired by &lt;a href="https://blog.briebug.com/blog/micro-frontends-angular" rel="noopener noreferrer"&gt;Briebug's post&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I've been listening much about the micro frontend concept lately. A lot of companies are adopting this solution as a way to make the apps smaller and easier to deploy.&lt;/p&gt;

&lt;h3&gt;
  
  
  But, what's micro frontend?
&lt;/h3&gt;

&lt;p&gt;According to the &lt;a href="https://micro-frontends.org/" rel="noopener noreferrer"&gt;micro-frontends&lt;/a&gt; website:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The idea behind Micro Frontends is to think about a website or web app as &lt;strong&gt;a composition of features&lt;/strong&gt; which are owned by &lt;strong&gt;independent teams&lt;/strong&gt;. Each team has a &lt;strong&gt;distinct area of business&lt;/strong&gt; or &lt;strong&gt;mission&lt;/strong&gt; it cares about and specialises in. A team is &lt;strong&gt;cross functional&lt;/strong&gt; and develops its features &lt;strong&gt;end-to-end&lt;/strong&gt;, from database to user interface.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's cool! With a micro frontend, we can split an application into smaller pieces that focus on solving a specific problem. It's perfect for companies that need to work with dedicated squads at specific parts of the product, like a cart or checkout module, for instance.&lt;/p&gt;

&lt;p&gt;This approach allows the squads to work with individual deploys and different stacks, without worrying about other projects that compose the entire solution.&lt;/p&gt;

&lt;h3&gt;
  
  
  What do we need to create a micro frontend app?
&lt;/h3&gt;

&lt;p&gt;To compose a micro frontend app, we'll just need to have two kinds of applications:&lt;/p&gt;

&lt;h4&gt;
  
  
  Host
&lt;/h4&gt;

&lt;p&gt;It's an application that will load the remote applications. We can have only one host app. This app can be called a &lt;strong&gt;shell&lt;/strong&gt; as well.&lt;/p&gt;

&lt;h4&gt;
  
  
  Remote
&lt;/h4&gt;

&lt;p&gt;It's an application that'll be loaded by the host application. We can have several of these apps. Generally, remote apps are small pieces of an application. Relevant modules that need some more attention and care.&lt;/p&gt;

&lt;h3&gt;
  
  
  Nice! And what do we're going to do?
&lt;/h3&gt;

&lt;p&gt;In this post, we're going to do a micro frontend application using the amazing &lt;a href="https://www.npmjs.com/package/@angular-architects/module-federation" rel="noopener noreferrer"&gt;Angular Module Federation&lt;/a&gt; module and &lt;a href="https://nx.dev/" rel="noopener noreferrer"&gt;Nx&lt;/a&gt; to create and manage the project's workspace.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you've never heard about Module Federation, &lt;a href="https://medium.com/swlh/webpack-5-module-federation-a-game-changer-to-javascript-architecture-bcdd30e02669" rel="noopener noreferrer"&gt;check out this post&lt;/a&gt; .&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Let's create the project then
&lt;/h3&gt;

&lt;p&gt;On the terminal, just type the command below and generate an Angular application.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npx create-nx-workspace@latest


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;On the &lt;em&gt;"application name"&lt;/em&gt; question, set the &lt;strong&gt;host&lt;/strong&gt; as a name. That'll be the host application.&lt;/p&gt;

&lt;p&gt;After that, you should have one app called &lt;strong&gt;host&lt;/strong&gt; inside the project's workspace.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fimg1.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fimg1.PNG" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let's create a new app called &lt;strong&gt;remote.&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

nx generate @nrwl/angular:application remote &lt;span class="nt"&gt;--port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;5001


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If everything worked fine, you'll have a new app inside the workspace.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fremote-app-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fremote-app-1.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All good?&lt;/p&gt;

&lt;p&gt;Perfect! In the next steps, we're going to transform these two apps into an amazing micro frontend solution.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting the Angular Module Federation
&lt;/h3&gt;

&lt;p&gt;It's time to add the Angular Module Federation to our project.&lt;/p&gt;

&lt;p&gt;Let's type the following command on the terminal:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

nx generate @nrwl/angular:setup-mf host &lt;span class="nt"&gt;--mf-type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;host &lt;span class="nt"&gt;--routing&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The following changes have been made inside the &lt;strong&gt;host&lt;/strong&gt; app:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A file called &lt;strong&gt;module-federation. config&lt;/strong&gt;  has been created&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;host&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;remotes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;remote&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;bootstrap&lt;/strong&gt; file contains the main's content file.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;main&lt;/strong&gt; file loads the &lt;strong&gt;bootstrap&lt;/strong&gt; file using an async import&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;host&lt;/strong&gt; app has become a shell app now - which means this app will be able to load remote apps.&lt;/p&gt;

&lt;p&gt;Good, let's transform the &lt;strong&gt;remote&lt;/strong&gt; app into a micro frontend app as well.&lt;/p&gt;

&lt;p&gt;Run the following command on the terminal:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

nx generate @nrwl/angular:setup-mf remote &lt;span class="nt"&gt;--mf-type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;remote &lt;span class="nt"&gt;--host&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;host &lt;span class="nt"&gt;--routing&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, just notice the &lt;strong&gt;remote&lt;/strong&gt; app. Inside the app folder, we have now a new folder called &lt;strong&gt;remote-entry,&lt;/strong&gt; and inside of it, there's an Angular module called &lt;strong&gt;RemoteEntryModule&lt;/strong&gt;. This module will allow us to load the remote app inside the host app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fremote-entry-module.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fremote-entry-module.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;entry.module.ts&lt;/strong&gt; file should contain this content:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;NgModule&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;declarations&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;RemoteEntryComponent&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;CommonModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;RouterModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forChild&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;RemoteEntryComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;]),&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RemoteEntryModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The &lt;strong&gt;RemoteEntryComponent&lt;/strong&gt; will be the component that'll be loaded inside the &lt;strong&gt;host&lt;/strong&gt; app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add some style to the remote component &lt;a href="https://emojipedia.org/dragon/" rel="noopener noreferrer"&gt;🐉&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;I think that'd be good to make some changes to this component for it to look nice.&lt;/p&gt;

&lt;p&gt;Don't worry! We'll just add a simple style to this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-entry&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`
    &amp;lt;div class="container"&amp;gt;
      &amp;lt;span&amp;gt;I'm a remote app :)&amp;lt;/span&amp;gt;
    &amp;lt;/div&amp;gt;
  `&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;`
      .container {
        border: 1px solid green;
        padding: 24px;
      }
    `&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RemoteEntryComponent&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Cool!&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's add a route to load de remote app
&lt;/h3&gt;

&lt;p&gt;Now, inside the &lt;strong&gt;host&lt;/strong&gt; app, let's add a route to load the &lt;strong&gt;remote&lt;/strong&gt; app.&lt;/p&gt;

&lt;p&gt;Add a file called &lt;strong&gt;app-routing.module.ts&lt;/strong&gt; inside the app folder, with the following content:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Routes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;remote-app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;loadChildren&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;remote/Module&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;RemoteEntryModule&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;NgModule&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;RouterModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;)],&lt;/span&gt;
    &lt;span class="na"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;RouterModule&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppRoutingModule&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;   


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Have you noticed? The route &lt;strong&gt;remote-app&lt;/strong&gt; will load the &lt;strong&gt;remote/Module&lt;/strong&gt; path. It's a file that webpack will create using the configuration file &lt;strong&gt;module-federation.config&lt;/strong&gt; inside &lt;strong&gt;remote app&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Oh! We can't forget to import the routing module into the AppModule.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;NgModule&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;declarations&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;AppComponent&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;BrowserModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="nx"&gt;AppRoutingModule&lt;/span&gt; &lt;span class="c1"&gt;// here&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
  &lt;span class="na"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;AppComponent&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Almost there!
&lt;/h3&gt;

&lt;p&gt;To finish the app, let's change de &lt;strong&gt;AppComponent's template&lt;/strong&gt; to something better&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;container&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;I&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;m the host app&amp;lt;/div&amp;gt;

    &amp;lt;a [routerLink]="[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;remote&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;]"&amp;gt;load the remote app&amp;lt;/a&amp;gt;

    &amp;lt;router-outlet&amp;gt;&amp;lt;/router-outlet&amp;gt; // the remote app comes here
&amp;lt;/div&amp;gt;


&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;

&lt;span class="nc"&gt;.container&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;24px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Time to see the result
&lt;/h3&gt;

&lt;p&gt;Well, now it's time to see the micro frontend application running. 😎&lt;/p&gt;

&lt;p&gt;Run the command below on the terminal:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;nx&lt;/span&gt; &lt;span class="nx"&gt;run&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;many&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;serve&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;all&lt;/span&gt; 


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Accessing the &lt;strong&gt;host app&lt;/strong&gt; we'll see the following page:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fhpst-app-page.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fhpst-app-page.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we click on the &lt;strong&gt;load the remote app&lt;/strong&gt; link, the &lt;strong&gt;host app&lt;/strong&gt; will load the &lt;strong&gt;remote app&lt;/strong&gt; using the &lt;strong&gt;/remote-app&lt;/strong&gt; route&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fezgif-5-2222.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.henriquecustodia.dev%2Fimages%2Fuploads%2Fezgif-5-2222.gif" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We made a micro frontend app! That's awesome how easy it was, isn't it?&lt;/p&gt;

&lt;h3&gt;
  
  
  Curious about the code?
&lt;/h3&gt;

&lt;p&gt;You can find the source code of this app on &lt;a href="https://github.com/henriquecustodia/mfe-post" rel="noopener noreferrer"&gt;my Github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you're curious to see the app running, I've deployed this to production.  &lt;a href="https://henriquecustodia-mf-host.netlify.app/" rel="noopener noreferrer"&gt;Check out the app here&lt;/a&gt;!&lt;/p&gt;

&lt;h3&gt;
  
  
  That's all
&lt;/h3&gt;

&lt;p&gt;I've spent some time writing this post, then, I hope that you've enjoyed it!&lt;/p&gt;

&lt;p&gt;Don't hesitate to share this post with your friends - I know that content can be helpful for lots of people.&lt;/p&gt;

&lt;p&gt;Thank you for the reading. 😄&lt;/p&gt;

&lt;p&gt;Bye! 👋🏼&lt;/p&gt;

</description>
      <category>angular</category>
      <category>microfrontend</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
