/********************ANGULAR WEB COMPONENT CREATION**************************/
1° START NEW ANGULAR PROJECT :
> ng new my-web-components --create-application=false
2° GENERATE WEB COMPONENT :
> ng generate application my-first-web-component --skipInstall=true
3° ADD @angular/elements LIBARARY
> ng add @angular/elements
4° GEREATE COMPONENT WISHED
> ng generate component my-component-generated
5° ADD SHADOW IN THE THE COMPONENT GENERATED (in my-component-generated.component.ts)
@Component({
selector: 'app-mon-composant',
template: 'Mon composant Angular',
encapsulation: ViewEncapsulation.ShadowDom //ONLY THIS IS ADDED
})
6° IN app.module.ts :
A) IMPORT Injector, DoBootstrap and createCustomElement AND REMOVE
> import { NgModule, Injector, DoBootstrap } from '@angular/core';
> import { BrowserModule } from '@angular/platform-browser';
> import { createCustomElement } from '@angular/elements';
B) IN @NgModule :
> ADD THE COMPONENT GENERATED WITH entryComponents: [myComponentGenerated]
> REMOVE providers: [] AND bootstrap: [AppComponent]
C ) REWRITE THE CLASS AppModule
- IMPLEMENTS DoBootstrap IN THE CONTRUCTOR AND REDIFINE IT
- ADD AN INJECTOR IN THE CONTRUCTOR
- ON ngDoBootstrap : INIT THE WEB COMPONENT CREATED
export class AppModule implements DoBootstrap{
constructor(private injector: Injector) {
}
ngDoBootstrap(): void {
this.createWebComponent(MySiteConfigurationComponent, 'my-component-alias');
}
createWebComponent(component:any, name: string) {
const webComponent = createCustomElement(component, {
injector: this.injector
});
customElements.define(name, webComponent);
}
}
7° ON angular.json : SET outputHashing TO NONE
> "outputHashing": "none",
8° ADD concat IN THE PROJECT :
> npm install concat
9° CREATE A NEW FILE(THAT WILL BE USED BY THE PROJECT USED THE WEB COMPONENT) build.js IN THE ROOT OF THE APPLICATION
> ADD THIS CODE :
const fs = require('fs');
const concat = require('concat');const distFolder = './dist/my-first-web-component/';(function build() {
concatFile();
})();function concatFile() {
const files = [
distFolder + 'runtime.js',
distFolder + 'polyfills.js',
distFolder + 'main.js'
]; concat(files, distFolder + 'component.js');
}
10° BUILD THE WEB COMPONENT :
> ng build mYFirstWebComponent
11° BUILD THE built.js :
> node built.js
12 ° NOW GO TO dist/my-first-web-component FOLDER AND COPY THIS FILES AND PAST THEM TO THE PROJECT USING THE WEB COMPONENT :
> component.js
> styles.css
13° IN THE PROJECT USING THE WEB COMPONENT :
> CREATE A FOLDER AND PAST FILES COPIED ON 12°
14° IN FILE OF THE PROJECT USING THE WEB COMPONENT :
INTEGRATION OF THE WEB COMPONENT :
A) IN THE HEADER :
A) IN THE body :