Table of Contents
How to Create a basic angular elements and use it in our application:
Angular elements are new HTML custom elements which are angular components packaged as custom elements.
Custom elements or web components give developers the ability to extend and create their own tags.It can be used without any dependencies. So you can use your custom elements in your React, Angular and Vue applications. You can also use it with your backend.
All the advantages of web components applies in Angular elements.
Why do we need it?
If your Application is small, then you can manage it easily. But If your Application is higher level which contains many sub applications with different frameworks then it is hard to move through.
Some of the modules/components which are used very often inside the application and every time we want to release a new version of this application then we also need to release and redeploy this sub application to get the newer version of application. Sometimes our sub application deployed once a week, or sometimes once a month so there is a situation when we have a different version of the same components, this can be handled by elements.
You can serve your element on the web. And your application references it just with the URL and gets the latest version of elements without need to redeploy. So you can release part of the application independently. So With the help of this complexity of application goes down.
With the help of Angular Elements we can also upgrade AngularJS application into Angular.
Angular Elements are:
- More reusable
- They are framework independent
- With the use of this we have less boilerplate
How to Create a basic angular elements and use it in our application:
Let's create angular elements project and add angular element in our project by
ng new angularElements
ng add @angular/elements
@angular/elements package creates a custom-element API which packs an angular component as a custom element.
Custom elements requires ES2015 classes. Support of custom elements and Es2015 may not be available in all the browser for that we need to use polyfill to support in that browser. For that we need to install webcomponents
npm install @webcomponents/custom-elements
After this package installed in your Application, open "pollyfill.js" and add this two lines in that file.
import '@webcomponents/custom-elements/src/native-shim';
import '@webcomponents/custom-elements/custom-elements.min';
To use a component as angular element we need to create component first or we can use existing components. Always use the ‘kebab-case’ form to generate components.
ng generate component register-form
To use our component as a custom elements:
Angular bundle include those components in the final bundle that has been referenced in the template, other components are ignored by it. So we need to tell angular to include this component in final bundle by adding them in entryComponent array of appModule. This component are Loaded dynamically.
Here I’m using AppComponent, RegisterFormComponent for custom elements.
ngDoBootstrap() inside AppModule tells Angular to handle bootstrapping for us. createCustomElement() method takes two parameters:
1. component name which you want to use as custom element
2. Injector: it’s responsible for creating instances of components as a custom element.
This process implements a constructor class which is a self-bootstrapping instance of your component. customElement.define() contains the name of the element.
Passing the string “app-main-page” inside customElements means that the <app-main-page> element is registered and we can use it inside DOM. We can change the name of the element inside define(), not required to be exact as angular component name. You can also write createCustomElement() and define() method inside constructor of the component.
Here I'm using an “app-main-page” element for the login screen in which I will show an app-register-form element if the user is not registered or the user clicks on the “Sign Up” button.
To use this element we have to add element tag name statically inside "index.html" using <script> tag or we can use it dynamically.
To Package all this thing:
Make build using angular-cli:
ng build --prod --output-hashing none
This will generate a dist folder with a set of javascript file. like this
So when we want to use our angular elements we have to add these JavaScript files in our project, and better solution for not to do this we can combine these files in one file and use it whenever we want to use our angular elements.
For that we require two additional packages "concat" and "fs-extra"
npm install --save-dev concat fs-extra
Now create a js file in root of your project folder, here I’m creating elements-build.js
const fs = require('fs-extra');
const concat = require('concat');
(async function build() {
const files = [
'./dist/angularElements/runtime-es2015.js',
'./dist/angularElements/runtime-es5.js',
'./dist/angularElements/main-es2015.js',
'./dist/angularElements/polyfills-es5.js',
'./dist/angularElements/scripts.js',
'./dist/angularElements/main-es5.js',
]
await fs.ensureDir('elements')
await concat(files, 'elements/framework-poll.js');
await fs.copyFile('./dist/angularElements/styles.css', 'elements/styles.css')
await fs.copy('./dist/angularElements/assets/', 'elements/assets/' )
})();
This will copy all the JavaScript files inside one file called “framework-poll.js” and also copy CSS files inside elements folder.
Now add this inside "package.json" which is run when we want to make build for elements.
"build:elements": "ng build --prod --output-hashing none && node elements-build.js"
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"build:elements": "ng build --prod --output-hashing none && node elements-build.js"
},
"private": true,
In terminal run "npm run build:elements" which will generate a dist and elements folder. Inside elements folders there’s “framework-poll,js” file which contains previous JavaScript files.
To use our angular elements we have to add angular elements tag name which we have define inside appModule with the above JavaScript file.
And our angular elements starts to work when we run this file.
To run this locally , we have to add index.html file where “framework-poll.js” placed. Here it is inside elements folder. And serve this file using http-server. Just ensure that tag name is same as define inside app.module.ts file.
Angular element is just an HTML element. So we can do all the things which we do on simple html tag like, set it’s attributes either by static declaration in template or by calling setAttribute method, interpolation, input and output operation. We can also hide or show based on condition like we do above for registration form.
Output for login screen, For login screen element tag name is <app-main-page>.
Output for registration screen, For this screen element tag name is <app-register-form> which is placed inside html file of app.component.html.
This screen opens when we click on Sign up button
This screen open when user is not registered and when he tries to login without registration.
Conclusion
This is how we can convert our standard angular components as angular element and use it with angular framework or any other framework.