<?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>Forem: Bruno Bradach</title>
    <description>The latest articles on Forem by Bruno Bradach (@brunob15).</description>
    <link>https://forem.com/brunob15</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%2F156670%2Fb23a7fec-7a43-4201-a759-9e58e5a725c6.png</url>
      <title>Forem: Bruno Bradach</title>
      <link>https://forem.com/brunob15</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/brunob15"/>
    <language>en</language>
    <item>
      <title>Lazy loading Angular components from Non-Angular applications</title>
      <dc:creator>Bruno Bradach</dc:creator>
      <pubDate>Sat, 21 Mar 2020 18:54:39 +0000</pubDate>
      <link>https://forem.com/brunob15/lazy-loading-angular-components-into-non-angular-applications-43k</link>
      <guid>https://forem.com/brunob15/lazy-loading-angular-components-into-non-angular-applications-43k</guid>
      <description>&lt;p&gt;Are you strugging to lazy load Angular components into your CMS platform or any other Non-Angular project? Then this article is for you!&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Introduction&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;I have been working with Adobe Experience Manager (AEM) for almost two years and most of the time I combined it with Angular as a frontend framework. One of my first tasks as an AEM developer was to find a good way to use Angular in conjunction with the CMS platform.&lt;/p&gt;

&lt;p&gt;At the time, Angular 6 had just been released and one of its main new features was Angular Elements. Angular Elements allows to create Web Components from Angular components and reuse them in other applications using different technologies. Since Web Components are framework agnostic and self-bootstrapped they are great for dynamic sites with CMS platforms.&lt;/p&gt;

&lt;p&gt;There are some articles you can read about Angular Elements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://juristr.com/blog/2019/04/intro-to-angular-elements/"&gt;https://juristr.com/blog/2019/04/intro-to-angular-elements/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.softwarearchitekt.at/aktuelles/angular-elements-part-i/"&gt;https://www.softwarearchitekt.at/aktuelles/angular-elements-part-i/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.softwarearchitekt.at/aktuelles/your-options-for-building-angular-elements/"&gt;https://www.softwarearchitekt.at/aktuelles/your-options-for-building-angular-elements/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Building&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;One of the good things of Web Components and custom elements is that you import some JS and CSS and you are ready to use them. With Angular you can &lt;code&gt;run ng build --prod&lt;/code&gt; and import the generated bundle files in other applications to use your custom elements.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UNBmAq6o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9w79dadyx31nsti3yclc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UNBmAq6o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9w79dadyx31nsti3yclc.png" alt="Build"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also you can use ngx-build-plus to build your custom elements. With this library you can get a single bundle file when building your project with Angular CLI.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;The problem&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Despite there are use cases where it could be handy to have all the components bundled to a single or a few files -like design systems-, there are other cases where it is not ideal.&lt;/p&gt;

&lt;p&gt;In my particular case I have an Angular project with about 20 -big- components that are included as custom elements in a dynamic site powered by Adobe Experience Manager. But, only one or two of those components are included in each page.&lt;/p&gt;

&lt;p&gt;So, if only one of the components is used within a page I would be delivering &lt;strong&gt;a lot&lt;/strong&gt; of unnecessary JavaScript to the browser.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Lazy loading&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Code splitting and lazy loading would help to tackle that problem. You can split your application into multiple NgModules accordingly.&lt;/p&gt;

&lt;p&gt;In my case, I could split up my project by creating a separate NgModule for each of my components and one or more shared modules to share features across the whole project. Now I would only need to lazy load them in order to lazy load my components.&lt;/p&gt;

&lt;p&gt;There are several options to lazy load components in Angular, for example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/juristr/ngx-lazy-el"&gt;ngx-lazy-el&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/angular-extensions/elements"&gt;angular-extensions/elements&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;But, how to lazy load the components from Non-Angular applications?&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;ngx-element&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;With &lt;a href="https://github.com/brunob15/ngx-element"&gt;ngx-element&lt;/a&gt; you can lazy load your Angular components from everywhere. That means from a CMS platform, a React application or just a plain HTML.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Repo: &lt;a href="https://github.com/brunob15/ngx-element"&gt;https://github.com/brunob15/ngx-element&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The library will define a custom element which you can pass a selector attribute to. That selector attribute determines what component you want to load. Also you can pass in attributes to your component by setting data-attributes to the custom element.&lt;/p&gt;

&lt;p&gt;Credits: Thanks to Juri Strumpflohner for the inspiration with ngx-lazy-el!&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Usage&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Let’s create a small Angular application to see ngx-element in action :) I am using Angular CLI v9.0.6. Choose SCSS as the css preprocessor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ng new lazy-components --minimal
$ cd lazy-components
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;We can remove &lt;code&gt;app.component.ts&lt;/code&gt; since we won’t need it and modify &lt;code&gt;app.module.ts&lt;/code&gt; accordingly.&lt;/p&gt;

&lt;p&gt;After doing that our &lt;code&gt;app.module.ts&lt;/code&gt; file should look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Basically I removed the &lt;code&gt;AppComponent&lt;/code&gt; and added the &lt;code&gt;ngDoBootstrap&lt;/code&gt; method since we are not bootsrapping any component in the module.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now let’s create a Talk component together with its feature module.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ng g module talk
$ ng g component talk
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;At the moment you should have the following folder structure:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PD-lacm5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/s13usksypvakbp1p6cqf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PD-lacm5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/s13usksypvakbp1p6cqf.png" alt="Folder structure with a feature module and a component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And your &lt;code&gt;talk&lt;/code&gt; files should look as follow:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Let’s change our &lt;code&gt;Talk&lt;/code&gt; component to make it display some information about a talk in a conference and give it some styles.&lt;/p&gt;

&lt;p&gt;Update the &lt;code&gt;talk.component.ts&lt;/code&gt; file to the following:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And create the following &lt;code&gt;talk.component.html&lt;/code&gt; and &lt;code&gt;talk.component.scss&lt;/code&gt; files next to &lt;code&gt;talk.component.ts&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Until now we have created a component that (trust me) will look like this later:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MlGfkeef--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/j6y93s74wwghkryew6vx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MlGfkeef--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/j6y93s74wwghkryew6vx.png" alt="Talk component"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So nothing strange until now, right? We have created a typical Angular application with an AppModule, a feature module and one component.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Remember that our goal is to use this component in Non-Angular applications and be able to lazy load it. We need Angular Elements and ngx-element in order to do that, so let’s put them in action…&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Install angular elements&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Angular provides a schematic to install and set up Angular Elements in our project. It will add a polyfill but it does not support IE11. If you need IE11 don’t use this schematic and see &lt;a href="https://www.softwarearchitekt.at/aktuelles/angular-elements-part-i"&gt;this article&lt;/a&gt; instead.&lt;/p&gt;

&lt;p&gt;Install Angular Elements by running &lt;code&gt;ng add @angular/elements&lt;/code&gt; in your terminal.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Install ngx-element&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;And install ngx-element by running &lt;code&gt;npm install ngx-element --save&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Expose the Talk component for ngx-element&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;In order to let ngx-element to access our component and create it on demand we need to make a couple of additions to our &lt;code&gt;talk.module.ts&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First we need to add &lt;code&gt;TalkComponent&lt;/code&gt; to the &lt;code&gt;entryComponents&lt;/code&gt; array. And second we are going to add a &lt;code&gt;customElementComponent&lt;/code&gt; property to the module in order to make the component’s class accessible to ngx-element.&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;talk.module.ts&lt;/code&gt; should be like this now:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Once we have done this we need to import and configure the &lt;code&gt;NgxElementModule&lt;/code&gt; in our &lt;code&gt;AppModule&lt;/code&gt; as follows:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h1&gt;
  
  
  &lt;strong&gt;Let’s test our component! 🎉&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;In order to test our component we are going to create some HTML where we can use it. Remember that we are not bootstrapping any Angular component and we are just adding custom elements to the DOM.&lt;/p&gt;

&lt;p&gt;Replace the &lt;code&gt;index.html&lt;/code&gt; file in the project with the following markup:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;And replace the global &lt;code&gt;styles.scss&lt;/code&gt; file with:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h1&gt;
  
  
  &lt;strong&gt;Run it! 🚀&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;At this point, if you run &lt;code&gt;ng serve&lt;/code&gt; in your terminal you should see our component in action:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WfkV3Cm_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ll4y745vsm58umv3btal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WfkV3Cm_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ll4y745vsm58umv3btal.png" alt="Talks"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And you can see that our Talk Module is being lazy loaded as we expected.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CsH7MYS1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/t8qw8tw23fw5xbfn7gks.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CsH7MYS1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/t8qw8tw23fw5xbfn7gks.png" alt="Lazy loaded bundle"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Play with it&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Now you can open your DevTools in the Network tab and verify that our &lt;code&gt;TalkModule&lt;/code&gt; is being lazy loaded.&lt;/p&gt;

&lt;p&gt;Some things you can play with to see the powers of custom elements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add a new talk to the DOM and see how it is self-bootstrapped.&lt;/li&gt;
&lt;li&gt;Change the &lt;code&gt;title&lt;/code&gt;, &lt;code&gt;description&lt;/code&gt; and &lt;code&gt;speaker&lt;/code&gt; attributes from the DevTools.&lt;/li&gt;
&lt;li&gt;Remove the talk custom elements from &lt;code&gt;index.html&lt;/code&gt; file and verify that the &lt;code&gt;TalkModule&lt;/code&gt; is not loaded initially. Then add a talk element to the DOM on the fly from the DevTools and verify that the &lt;code&gt;TalkModule&lt;/code&gt; is lazy loaded.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Conclusions&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;With ngx-element we have built a component and leveraged all benefits of Angular framework, custom elements and lazy loading.&lt;/p&gt;

&lt;p&gt;This library has changed the way I integrate &lt;em&gt;Angular&lt;/em&gt; and &lt;em&gt;Adobe Experience Manager&lt;/em&gt; for the better. I hope this can be useful for developers trying to use Angular as a frontend framework together with CMS platforms or any other Non-Angular projects.&lt;/p&gt;

&lt;p&gt;Thank you for reading 🙂&lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>cms</category>
      <category>webcomponents</category>
    </item>
  </channel>
</rss>
