{"id":576,"date":"2023-02-28T05:00:00","date_gmt":"2023-02-28T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=576"},"modified":"2023-02-27T19:05:34","modified_gmt":"2023-02-28T03:05:34","slug":"adding-dependencies-to-standalone-components","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/adding-dependencies-to-standalone-components\/","title":{"rendered":"Adding dependencies to standalone components"},"content":{"rendered":"\n<p>Now that we&#8217;ve introduced <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/what-are-standalone-components\/\" target=\"_blank\" rel=\"noopener\" title=\"\">standalone components<\/a>, you might have tested them and quickly realized that if you start using directives such as <code>*ngIf<\/code>, or other components, your code doesn&#8217;t compile anymore.<\/p>\n\n\n\n<p>That&#8217;s because those template dependencies (used only in the HTML template of your component) are not imported in Typescript (yet), so Angular cannot compile your templates. This doesn&#8217;t happen when we use modules because our components are declared there. We also import CommonModule by default, which contains all of the primary directives and pipes of the Angular framework.<\/p>\n\n\n\n<p>If we want to import all these features into our standalone components, we can use the <code>import<\/code> property in the decorator as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"303\" height=\"116\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-62.png\" alt=\"\" class=\"wp-image-578\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-62.png 303w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-62-300x115.png 300w\" sizes=\"auto, (max-width: 303px) 100vw, 303px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>And if you want to import just one feature instead of an entire module, you can do that too &#8211; but <strong>only if that feature is declared as standalone<\/strong>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"273\" height=\"119\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-63.png\" alt=\"\" class=\"wp-image-579\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>This means that <b>the Angular team has modified all Angular directives and pipes<\/b> to be available as standalone features (see the <a href=\"https:\/\/github.com\/angular\/angular\/blob\/15.2.0\/packages\/common\/src\/directives\/ng_if.ts#L149-155\" target=\"_blank\" rel=\"noopener\" title=\"\">ngIf source code here<\/a> as an example).<\/p>\n\n\n\n<p>Of course, we can still import entire modules if needed or list individual template dependencies, which means that all pipes, directives, and components should be listed individually in the <code>imports<\/code> array:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-64.png\" alt=\"\" class=\"wp-image-582\" width=\"337\" height=\"32\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-64.png 337w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-64-300x28.png 300w\" sizes=\"auto, (max-width: 337px) 100vw, 337px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>You can see an <a href=\"https:\/\/stackblitz.com\/edit\/standalone-button-with-deps?file=src%2Fbutton%2Fbutton.component.ts,src%2Fbutton%2Fbutton.component.html,src%2Fmain.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">example on Stackblitz here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now that we&#8217;ve introduced standalone components, you might have tested them and quickly realized that if you start using directives such as *ngIf, or other components, your code doesn&#8217;t compile anymore. That&#8217;s because those template dependencies (used only in the HTML template of your component) are not imported in Typescript (yet), so Angular cannot compile [&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-576","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\/576","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=576"}],"version-history":[{"count":7,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/576\/revisions"}],"predecessor-version":[{"id":589,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/576\/revisions\/589"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}