{"id":570,"date":"2023-02-27T05:00:00","date_gmt":"2023-02-27T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=570"},"modified":"2023-02-27T19:22:42","modified_gmt":"2023-02-28T03:22:42","slug":"what-are-standalone-components","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/what-are-standalone-components\/","title":{"rendered":"What are standalone components?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Since Angular 14, any Angular feature (component, pipe, or directive) can be created as &#8220;<a href=\"https:\/\/angular.io\/guide\/standalone-components\" target=\"_blank\" rel=\"noopener\" title=\"\">standalone<\/a>.&#8221; This week, we will dive into what standalone components are, what feature they bring to the table, and how to use them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One quick note before we start: Whenever you see the words <strong>&#8220;standalone components,&#8221;<\/strong> it really means &#8220;standalone components\/pipes\/directives.&#8221; Perhaps we should call those &#8220;standalone features,&#8221; but I&#8217;ll stick with the naming convention used by the Angular team so far, so I don&#8217;t stand&#8230; alone. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s a standalone component?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A standalone component is a component that doesn&#8217;t belong to any <code>NgModule<\/code>. It can be imported on its own and used as-is.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance, in the past, we might have a <code>ButtonComponent<\/code> and a <code>ButtonDirective<\/code> in a <code>ButtonModule<\/code> (just like <a href=\"https:\/\/material.angular.io\/components\/button\/api\" target=\"_blank\" rel=\"noopener\" title=\"\">Angular Material<\/a> does). This means that if we want to use <code>ButtonComponent<\/code>, we have to import the <code>ButtonModule<\/code> in our <code>AppModule<\/code> or feature module. This will make both <code>ButtonComponent<\/code> and <code>ButtonDirective<\/code> available for use in our app, even if you just use one of those features and don&#8217;t need the other. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Standalone components are different. They can be imported in a module just like other modules get imported in the array of <code>imports<\/code>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"434\" height=\"32\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-60.png\" alt=\"\" class=\"wp-image-571\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-60.png 434w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-60-300x22.png 300w\" sizes=\"auto, (max-width: 434px) 100vw, 434px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Importing only the features we need is always better for performance, as the build output will be smaller than if we import an entire module of dependencies. So that would be benefit number one of standalone components.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to create a standalone component?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With the Angular CLI: <code>ng generate component Button --standalone<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can also make an existing component standalone by adding the <code>standalone: true<\/code> property in the <code>@Component<\/code> decorator like so:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"513\" height=\"221\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-61.png\" alt=\"\" class=\"wp-image-573\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-61.png 513w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-61-300x129.png 300w\" sizes=\"auto, (max-width: 513px) 100vw, 513px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can see that <a href=\"https:\/\/stackblitz.com\/edit\/angular-ahmn7j?file=src%2Fbutton%2Fbutton.component.ts,src%2Fbutton%2Fbutton.component.html,src%2Fmain.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">example in action here on Stackblitz<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since Angular 14, any Angular feature (component, pipe, or directive) can be created as &#8220;standalone.&#8221; This week, we will dive into what standalone components are, what feature they bring to the table, and how to use them. One quick note before we start: Whenever you see the words &#8220;standalone components,&#8221; it really means &#8220;standalone components\/pipes\/directives.&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,21,8,23],"tags":[],"class_list":["post-570","post","type-post","status-publish","format-standard","hentry","category-angular","category-architecture","category-components","category-standalone"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=570"}],"version-history":[{"count":4,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/570\/revisions"}],"predecessor-version":[{"id":597,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/570\/revisions\/597"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}