{"id":596,"date":"2023-03-02T05:00:00","date_gmt":"2023-03-02T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=596"},"modified":"2023-03-01T19:26:42","modified_gmt":"2023-03-02T03:26:42","slug":"lazy-loading-standalone-components","status":"publish","type":"post","link":"http:\/\/www.angulartraining.com\/daily-newsletter\/lazy-loading-standalone-components\/","title":{"rendered":"Lazy-loading standalone components"},"content":{"rendered":"\n<p>Now that we&#8217;ve covered both <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/what-are-standalone-components\/\" target=\"_blank\" rel=\"noopener\" title=\"\">standalone components<\/a> and <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/lazy-loading-for-better-angular-performance\/\" target=\"_blank\" rel=\"noopener\" title=\"\">lazy-loading Angular modules<\/a>, we can introduce the concept of <a href=\"https:\/\/angular.io\/guide\/standalone-components#lazy-loading-a-standalone-component\" target=\"_blank\" rel=\"noopener\" title=\"\">lazy-loading a standalone component<\/a>, which wasn&#8217;t possible before Angular 14.<\/p>\n\n\n\n<p>From a syntax standpoint, the only difference is that we use <code>loadComponent<\/code> instead of <code>loadChildren<\/code>. Everything else remains the same in terms of route configuration:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"66\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-66-1024x66.png\" alt=\"\" class=\"wp-image-598\" srcset=\"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-66-1024x66.png 1024w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-66-300x19.png 300w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-66-768x49.png 768w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-66.png 1498w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Now, one of the benefits of lazy-loading a module is that it can have its own routing config, thus <a href=\"https:\/\/angular.io\/guide\/lazy-loading-ngmodules#inside-the-feature-module:~:text=The%20feature%2Dspecific%20route%20definition%20file%20customers%2Drouting.module.ts%20imports%20its%20own%20feature%20component%20defined%20in%20the%20customers.component.ts%20file%2C%20along%20with%20the%20other%20JavaScript%20import%20statements.%20It%20then%20maps%20the%20empty%20path%20to%20the%20CustomersComponent.\" target=\"_blank\" rel=\"noopener\" title=\"\">lazy-loading multiple components for different routes at once<\/a>.<\/p>\n\n\n\n<p>The good news is that we can also lazy-load multiple standalone components. All it takes is creating a specific routing file that we point <code>loadChildren<\/code> to, like so:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"502\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-67-1024x502.png\" alt=\"\" class=\"wp-image-599\" srcset=\"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-67-1024x502.png 1024w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-67-300x147.png 300w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-67-768x377.png 768w, http:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-67.png 1358w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>One last cool thing to share today: Along with the above syntax, Angular now supports default exports in Typescript with both <code>loadChildren<\/code> and <code>loadComponent<\/code>. <\/p>\n\n\n\n<p>This means that the previous verbose syntax:<\/p>\n\n\n\n<p><code>loadComponent: () =&gt; import('.\/admin\/panel.component').then(mod =&gt; mod.AdminPanelComponent)},<\/code><\/p>\n\n\n\n<p>Can now become:<\/p>\n\n\n\n<p><code>loadComponent: () =&gt; import('.\/admin\/panel.component')<\/code><\/p>\n\n\n\n<p>This works if that component is the default export in its file, meaning that the class declaration looks like this:<\/p>\n\n\n\n<p><code>export default class PanelComponent<\/code><\/p>\n\n\n\n<p>The same applies to <code>loadChildren<\/code> if the array of routes (or <code>NgModule<\/code>) is the default export in its file. You can see an <a href=\"https:\/\/stackblitz.com\/edit\/at-lazy-load-default?file=src%2Fapp%2Fapp-routing.module.ts,src%2Fapp%2Forders%2Forders.module.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">example in Stackblitz here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now that we&#8217;ve covered both standalone components and lazy-loading Angular modules, we can introduce the concept of lazy-loading a standalone component, which wasn&#8217;t possible before Angular 14. From a syntax standpoint, the only difference is that we use loadComponent instead of loadChildren. Everything else remains the same in terms of route configuration: Now, one of [&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,20,23],"tags":[],"class_list":["post-596","post","type-post","status-publish","format-standard","hentry","category-angular","category-architecture","category-components","category-performance","category-standalone"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=596"}],"version-history":[{"count":5,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/596\/revisions"}],"predecessor-version":[{"id":605,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/596\/revisions\/605"}],"wp:attachment":[{"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=596"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}