{"id":2970,"date":"2023-11-15T23:07:59","date_gmt":"2023-11-15T22:07:59","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=2970"},"modified":"2023-11-15T23:31:00","modified_gmt":"2023-11-15T22:31:00","slug":"how-to-bootstrapping-standalone-components-in-angular","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2023\/11\/15\/how-to-bootstrapping-standalone-components-in-angular\/","title":{"rendered":"How to bootstrapping Standalone Components in Angular"},"content":{"rendered":"\n<p>The standalone component approach differs from using the&nbsp;<code>NgModule<\/code>&nbsp;approach the moment we want to bootstrap.<\/p>\n\n\n\n<p>With&nbsp;<code><strong>NgModule<\/strong><\/code>, this is done via a&nbsp;<code><strong>bootstrapModule<\/strong><\/code>&nbsp;function exported from the&nbsp;<code><strong>@angular\/platform-browser-dynamic<\/strong><\/code>&nbsp;package:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { NgModule } from '@angular\/core';\nimport { platformBrowserDynamic } from '@angular\/platform-browser-dynamic';\n\n@NgModule({...})\nexport class AppModule {}\n\nplatformBrowserDynamic()\n  .bootstrapModule(AppModule)\n  .catch((err) =&gt; console.error(err));<\/code><\/pre>\n\n\n\n<p>With standalone components, there are a few changes. Instead of using the&nbsp;<code>@angular\/platform-browser-dynamic<\/code>&nbsp;package, we\u2019ll refactor it to<strong>&nbsp;<code>@angular\/platform-browser<\/code><\/strong>&nbsp;instead.<\/p>\n\n\n\n<p>This exposes to us a new function called&nbsp;<code>bootstrapApplication<\/code>, which expects a&nbsp;<code>@Component<\/code>&nbsp;instead of&nbsp;<code>@NgModule<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { Component } from '@angular\/core';\nimport { bootstrapApplication } from '@angular\/platform-browser';\n\n@Component({...})\nexport class AppComponent {}\n\nplatformBrowserDynamic()\n  .bootstrapApplication(AppComponent)\n  .catch((err) =&gt; console.error(err));<\/code><\/pre>\n\n\n\n<p>As this returns us a&nbsp;<code>Promise<\/code>&nbsp;as well, we can also use it to catch any errors like with&nbsp;<code>NgModule<\/code>.<\/p>\n\n\n\n<p><strong>PS: <\/strong>Let us deep analyse &nbsp;the function signature for&nbsp;<code>bootstrapApplication<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bootstrapApplication(rootComponent: Type&lt;unknown&gt;, options?: ApplicationConfig | undefined): Promise&lt;ApplicationRef&gt;<\/code><\/pre>\n\n\n\n<p>Here we expect a&nbsp;<strong><code>rootComponent<\/code>,<\/strong> which must be a standalone component. We\u2019ll see an error if the rootComponent is not  a standalone component, for that we\u2019ll need to add&nbsp;<code>standalone: true<\/code>&nbsp;into the&nbsp;<code>@Component<\/code>&nbsp;metadata.<\/p>\n\n\n\n<p>we\u2019ll also see the&nbsp;<strong><em><code>options?: ApplicationConfig | undefined<\/code>&nbsp;<\/em><\/strong>which is an&nbsp;<code>interface<\/code>&nbsp;containing only a&nbsp;<code>providers<\/code>&nbsp;property.<\/p>\n\n\n\n<p>This is where we can then pass any providers we want to make available in our components and children ( for example with the help of <strong><em>importProvidersFrom()<\/em><\/strong> method) . <\/p>\n\n\n\n<p><strong><em>example:<\/em><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bootstrapApplication(AppComponent, {\r\n  providers: &#91;importProvidersFrom(HttpClientModule), AuthService],\r\n}).catch((err) => console.error(err));<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The standalone component approach differs from using the&nbsp;NgModule&nbsp;approach the moment we want to bootstrap. With&nbsp;NgModule, this is done via a&nbsp;bootstrapModule&nbsp;function exported from the&nbsp;@angular\/platform-browser-dynamic&nbsp;package: With standalone components, there are a few changes. Instead of using the&nbsp;@angular\/platform-browser-dynamic&nbsp;package, we\u2019ll refactor it to&nbsp;@angular\/platform-browser&nbsp;instead. This exposes to us a new function called&nbsp;bootstrapApplication, which expects a&nbsp;@Component&nbsp;instead of&nbsp;@NgModule: As this returns us [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2038,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[37],"tags":[109,755,754],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2970"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=2970"}],"version-history":[{"count":5,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2970\/revisions"}],"predecessor-version":[{"id":3005,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2970\/revisions\/3005"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/2038"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=2970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}