<?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: Stefan</title>
    <description>The latest articles on DEV Community by Stefan (@riget75).</description>
    <link>https://dev.to/riget75</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%2F1229811%2F2fa21337-ac02-4873-8e6d-ad8fbd59630c.jpg</url>
      <title>DEV Community: Stefan</title>
      <link>https://dev.to/riget75</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/riget75"/>
    <language>en</language>
    <item>
      <title>Einführung in Angular Signals</title>
      <dc:creator>Stefan</dc:creator>
      <pubDate>Mon, 15 Jan 2024 16:47:18 +0000</pubDate>
      <link>https://dev.to/riget75/einfuhrung-in-angular-signals-2je6</link>
      <guid>https://dev.to/riget75/einfuhrung-in-angular-signals-2je6</guid>
      <description>&lt;p&gt;Angular hat mit Version 16 als Developer Preview und ab Angular 17 fix integriert eine neue Möglichkeit zur reaktiven Programmierung eingeführt, die Signals:&lt;/p&gt;

&lt;p&gt;Ein Signal ist ein Wrapper um einen Wert, der interessierte Verbraucher benachrichtigen kann, wenn sich dieser Wert ändert. Signale können jeden Wert enthalten, von einfachen Grundelementen bis hin zu komplexen Datenstrukturen.&lt;/p&gt;

&lt;p&gt;Der Wert eines Signals wird immer über eine Getter-Funktion gelesen, die es Angular ermöglicht, zu verfolgen, wo das Signal verwendet wird.&lt;/p&gt;

&lt;p&gt;Signals sind somit eine Möglichkeit, Wert-Änderungen zu den Templates zu übertragen und Angular die Möglichkeit zu geben, nur den Teil des DOM zu aktualisieren, welche diesen Wert auch verwenden. Um dies zu verstehen, müssen wir uns zuerst ansehen, wie Angular bisher wußte, wann die Bereiche der Website zu aktualisieren sind:&lt;/p&gt;

&lt;h2&gt;
  
  
  Angular Change Detection und Signals
&lt;/h2&gt;

&lt;p&gt;Bisher baute Angular auf einer Library namens zone.js auf. Diese Library machte es grob gesagt möglich, auf jegliche Änderungen im Browser (z.B. ein Button-Klick, eine Formular-Eingabe usw.) reagieren zu können. Sobald eine Änderung ausgelöst wird, geht Angular seine Komponenten im DOM durch und prüft, ob etwas aktualisiert werden muss. Vorteil dieser Methode ist, dass man sich als Programmierer nicht darum kümmern musste. D.h. wenn ich z.B. den Wert einer Variable nach einem Button-Klick ändere und diese im Template angezeigt wird (Binding), ist diese Änderung automatisch sichtbar.&lt;/p&gt;

&lt;p&gt;Performance-mäßig ist diese Methode jedoch nie ganz optimal gewesen. Es gibt verschiedene Methoden, um hier Verbesserungen zu bewirken, z.B. indem man seine Komponente mit der OnPush-Strategie kennzeichnet. Danach nimmt die Komponente am Change-Detection Zyklus nur mehr teil, wenn sich seine @Input-Variablen geändert haben.&lt;/p&gt;

&lt;p&gt;Mit Signals hat Angular eine neue Methode eingeführt, wie man Angular mitteilen kann, was bei Änderung eines Wertes aktualisiert werden muss.&lt;/p&gt;

&lt;h2&gt;
  
  
  Was sind Signals?
&lt;/h2&gt;

&lt;p&gt;Ein Signal in Angular besteht immer aus einem Produzenten und einem Konsumenten. Der Produzent erzeugt Daten und der Konsument liest diese Daten. D.h. jedes mal, wenn sich der Wert eines Signals ändert, werden alle Konsumenten darüber informiert und können dementsprechend den neuen Wert verwenden. Signals haben im Prinzip große Ähnlichkeit mit BehaviorSubject’s der rxjs-Bibliothek.&lt;/p&gt;

&lt;p&gt;Weiters wird unterschieden zwischen schreibenden Signalen und berechneten Signalen. Ein schreibendes Signal gibt den Wert direkt weiter, während ein berechnetes Signal eine Funktion ist, welche schreibende Signale verwendet und diese modifizieren oder kombinieren kann.&lt;/p&gt;

&lt;h2&gt;
  
  
  Beispiel für die Verwendung von Angular Signals
&lt;/h2&gt;

&lt;p&gt;Wir wollen uns ein einfaches Beispiel ansehen. Wir haben einen Button, welcher auf Klick eine Zufallszahl erzeugt und sowohl diese als auch den doppelten Wert anzeigt. Um Signals zu verwenden, müssen diese aus der @angular/core-Bibliothek importiert werden. Wir verwenden sowohl eine &lt;strong&gt;schreibendes Signal (signal)&lt;/strong&gt; als auch ein &lt;strong&gt;berechnetes (computed)&lt;/strong&gt;:&lt;br&gt;
&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;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;computed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&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="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-signal-beispiel&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="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`
      &amp;lt;div&amp;gt;
          Value: {{value()}}
          &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;
          Value doubled: {{valueDoubled()}}
          &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;
      &amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;
          &amp;lt;button (click)="onClick()"&amp;gt;Zufallszahl erzeugen&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
  `&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styleUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./signal-beispiel.component.css&lt;/span&gt;&lt;span class="dl"&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;SignalBeispielComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;valueDoubled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;computed&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;value&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&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;ul&gt;
&lt;li&gt;Mit &lt;code&gt;import {Component, computed, signal} from '@angular/core';&lt;/code&gt; werden die Signal und Computed-Klassen importiert&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;public value = signal(0);&lt;/code&gt; erzeugt ein neues Signal mit dem Initial-Wert 0. Signals haben immer einen definierten Anfangswert.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;public valueDoubled = computed(() =&amp;gt; this.value() * 2);&lt;/code&gt; erzeugt ein berechnetes Signal, welches das "value"-Signal verwendet und daraus einen neuen Wert berechnet, in diesem Fall einfach verdoppelt.&lt;/li&gt;
&lt;li&gt;Mit &lt;code&gt;this.value.set(....)&lt;/code&gt; wird ein neuer Wert für das Signal gesetzt, und damit auch automatisch eine neue Berechnung des computed-Signal „valueDoubled“ ausgelöst.&lt;/li&gt;
&lt;li&gt;Die Ausgabe im Template erfolgt mit &lt;code&gt;Value: {{value()}}&lt;/code&gt;. Zu beachten ist die Funktionsklammern nicht vergessen, es handelt sich beim Signal nicht um eine einfache Variable!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hier finden Sie das &lt;a href="https://stackblitz.com/edit/stackblitz-starters-qj2brx?file=src%2Fsignal-beispiel%2Fsignal-beispiel.component.ts"&gt;Beispiel auf StackBlitz&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Die Benutzung von Signals ist also sehr einfach. Der Vorteil für das Framework ist, mit der Verwendung des Signals im Template wird die Komponente automatisch markiert für die Change-Detection, d.h. Angular weiß sofort, wo es sich auswirkt wenn das Signal einen neuen Wert produziert.&lt;/p&gt;

&lt;h2&gt;
  
  
  Möglichkeiten zum Setzen eines Wertes des Signals
&lt;/h2&gt;

&lt;p&gt;Wir haben schon eine Methode kennengelernt um den Wert eines Signals zu setzen: .set(…). Es gibt aber auch die Möglichkeit, mit .update() einen Wert basierend auf den aktuellen zu setzen:&lt;/p&gt;

&lt;p&gt;Nehmen wir an, wir hätten eine Book-Klasse und definieren ein schreibbares Signal als Array von Büchern:&lt;br&gt;
&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;public&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;WritableSignal&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;([]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Dann können wir ein neues Buch folgendermaßen hinzufügen:&lt;br&gt;
&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;book&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Book&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;Book&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Neuer Buch-Titel&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;books&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;books&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;book&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Typ eines Signals definieren
&lt;/h2&gt;

&lt;p&gt;Wie wir im obigen Beispiel gesehen haben, kann man den Datentyp eines Signals als WritableSignal deklarieren.&lt;br&gt;
&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;public&lt;/span&gt; &lt;span class="nx"&gt;books&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;WritableSignal&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;([]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Berechnete Signale (computed)
&lt;/h2&gt;

&lt;p&gt;Ein berechnetes Signal setzt sich aus einem oder mehreren Signalen zusammen und berechnet daraus einen neuen Wert. Es wird automatisch erkannt, welche Signale verwendet werden und das computed-Signal nur dann neu berechnet, wenn sich einer oder mehrere der verwendeten Signale im Wert ändern.&lt;/p&gt;

&lt;p&gt;Beispiel:&lt;br&gt;
&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;public&lt;/span&gt; &lt;span class="nx"&gt;wert1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;wert2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;summe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;computed&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wert1&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;wert2&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hier würde summe immer nur dann ausgeführt, wenn sich eines der beiden Signale wert1 oder wert2 im Wert ändert.&lt;/p&gt;

&lt;h2&gt;
  
  
  Effekte
&lt;/h2&gt;

&lt;p&gt;Effekte im Zusammenhang mit Signals sind Programmcode, welcher immer dann ausgeführt wird, wenn sich ein- oder mehrere Signals-Werte ändern. Im Gegensatz zu computed wird hier aber kein Wert zurückgegeben, sondern nur der Programm-Code ausgeführt. Effekte eigenen sich also gut dazu, z.B. Signal-Werte zu loggen.&lt;/p&gt;

&lt;p&gt;Beispiel:&lt;br&gt;
&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;public&lt;/span&gt; &lt;span class="nx"&gt;wert&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;logEffect&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;effect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Wert hat sich geändert: ${this.wert()}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Fazit
&lt;/h2&gt;

&lt;p&gt;Angular bietet mit Signals eine neue und einfache Möglichkeit zur reaktiven Programmierung an. Vor allem für Einsteiger ist das Konzept leichter Verständlich als die vielen Möglichkeiten, die z.B. rxjs mit den Observables bietet. Signals erweitern die Möglichkeiten, große Angular Anwendungen geschwindigkeitsmäßig zu optimieren und die Change-Detection zu beschleunigen.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>signals</category>
      <category>deutsch</category>
    </item>
    <item>
      <title>Standalone components in Angular</title>
      <dc:creator>Stefan</dc:creator>
      <pubDate>Mon, 11 Dec 2023 17:14:48 +0000</pubDate>
      <link>https://dev.to/riget75/standalone-components-in-angular-2kea</link>
      <guid>https://dev.to/riget75/standalone-components-in-angular-2kea</guid>
      <description>&lt;p&gt;Angular has allowed the declaration and use of so-called standalone components for some time now (from Angular 14 on). These are components independent of a module and can therefore be used without the need of declaring them in any module.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to create a standalone component
&lt;/h2&gt;

&lt;p&gt;Creating standalone components is in principle very easy. You just have to set the &lt;code&gt;standalone&lt;/code&gt; flag in the component decorator to true. But now that the component is no longer embedded in a module, so you also have to import all services and other components used:&lt;br&gt;
&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;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;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-my-component&lt;/span&gt;&lt;span class="dl"&gt;'&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;RouterOutlet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="nx"&gt;ReminderComponent&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;./app.my-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;./app.my-component.css&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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppComponent&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 imports declaration specifies which other building blocks this component is allowed to use. These can be other NgModules but also other standalone components.&lt;/p&gt;

&lt;p&gt;The standalone component already has all the information to act as an independent unit. It is not dependent on a module and does not have to be declared in a module. You can imagine a standalone component as a module and component combined in a single unit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bootstrap Angular via a standalone component
&lt;/h2&gt;

&lt;p&gt;Before standalone components were possible, you had to specify the start module in the main.ts, which in turn had to declare the start component. Normally this was AppModule and AppComponent if you created a project with ng.&lt;/p&gt;

&lt;p&gt;With the introduction of standalone components, the application can now be bootstraped with a standalone component. There is a new method for this: &lt;code&gt;boostrapApplication&lt;/code&gt;. The first argument to this command is the start component, as the second argument you can specify the service providers used across the application, which you would previously register in the AppModule.&lt;br&gt;
&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;// main.ts&lt;/span&gt;
&lt;span class="k"&gt;import&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;AppComponent&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="nf"&gt;importProvidersFrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;HttpClientModule&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
     &lt;span class="nf"&gt;provideRouter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;APP_ROUTES&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
     &lt;span class="nf"&gt;provideAnimations&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;This means that a central AppModule is no longer necessary for an Angular application. As you can also see here, you can import the providers of existing ngModules with the command&lt;/p&gt;

&lt;p&gt;&lt;code&gt;importProvidersFrom&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This means that the modular and standalone approaches can coexist. So you don't have to decide on an approach overnight and compatibility with existing module code is still guaranteed.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>standalonecomponents</category>
    </item>
  </channel>
</rss>
