I built a simple Web Component via Angular using Pascal Precht's tutorial, which you can see working HERE. It auto-magically compiles in the on Stackblitz in the link, but not locally.
My end goal is to have the code for the resulting Web Component in a separate file locally. Eventually, I will upload it somewhere and pull it in via a single <script>
tag, just like normal raw-html/javascript Web Components. I think the question speaks for itself, but you can read the details below if you would like:
Details:
To summarize my code in the link above, I have a very basic component:
import { Component } from '@angular/core';
@Component({
selector: 'hello-world',
template: `<h1>Hello world</h1>`
})
export class HelloComponent {}
and I have a module:
import { NgModule, Injector } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { createCustomElement } from '@angular/elements'
import { HelloComponent } from './hello.component';
@NgModule({
imports: [BrowserModule],
declarations: [HelloComponent],
entryComponents: [HelloComponent]
})
export class AppModule {
constructor(private injector: Injector) {}
ngDoBootstrap() {
const HelloElement = createCustomElement(HelloComponent, {
injector: this.injector
});
customElements.define('hello-world', HelloElement);
}
}
Here is an explanation of the module above:
- Add my component to the
entryComponents
array so it's not taken out by the angular tree-shaker (since it's not reachable on app boot:entryComponents: [HelloComponent]
Run my component through the
createCustomElement
function so that I can use it as a regular htmlWeb Component
:const HelloElement = createCustomElement(HelloComponent, { injector: this.injector });
Finally, I ask Angular to compile this component in
main.ts
:platformBrowserDynamic().bootstrapModule(AppModule);
Here is the stuff I read / watched fully (among dozens of other links - most of which are dated, like the original Angular Elements intro):
Web Components from Scratch by Tomek Sułkowski (He never compiles it separately)
Web Components with CLI (Same problem)
Web Components by Academind (Yet again, this guy also uses them within Angular apps)
Thank you for any help.
make sure you use
npm install --save @angular/elements
and add"@webcomponents/custom-elements" : "^1.0.8"
in package.json. After that runnpm install
& along with that you need to un-comment below lines from polyfills.tsThis adds a polyfill which is required for custom elements to work.
import '@webcomponents/custom-elements/custom-elements.min'; import '@webcomponents/custom-elements/src/native-shim';
<my-tag message="This is rendered dynamically">stack Overflow</my-tag>
Angular doesn't compile this above code, but angular elements fixes this issue by allowing to take our angular component and put it into totally encapsulated self bootstrapping HTML element which you can dump into your angular app in this following way for e.g and which will still work.
In AppComponent.ts file
And inside your
HelloComponent
Now this is loaded as native web component.Still only usable in angular projects, but already usable for dyanamic content like this.
I hope this will help you to run your code locally
Hello there.
If i am understanding correctly, you want to generate a Web Component (lets say
<my-component></my-component
) and then with a simple script tag to get the .js file to initialize that component and add it on any html page you want to.In my GitHub repository i have created a simple Todo List Component. That component follows the Angular Elements Principles and also, i have installed some file managment libraries for webpack to also pack the JS into one JS file.
You can check this repository out and see if that helps you out. Just clone it and then run npm install followed by npm run build:elements Feel free to contact me if anything goes south.
Also check this guide. This guy has helped me a lot.
Best of luck
From what I read the packaging specific for Angular Elements components for easy use outside Angular will come with Angular 7.
What you can do now is to create and angular application with the cli.
Add the Angular Elements library with:
This adds also all required polyfills as described in the official Angular Documentation.
Then you change the AppModule to not be a bootstrap module but just register the custom elements. You remove the bootstrap from the NgModule and ad the components as entry components. Then register the components as custom elements in the
ngDoBootstrap
hook. I made both the default AppComponent and HelloComponent custom elements. This is how my app module looks like:Then you can use the elements in the index.html like elements for example like this:
If you build this with
ng build --prod
you get minimized packages that you could use also now in other html pages by including the package scripts as they get included by the compiler in the index.html file.I have added my sample to GitHub. In the history you can see what I have changed from the initial cli application.
Current Angular version doesn’t provide an option to export component as single local file which can used in any non angular application. However it can be achieved by making changes in building and deployment steps. In my example I have created two angular elements a button and alert message. Both components are compiled and exported as single local file which I’m loading in a plain html file with javascript.
Here are the steps follows: 1. Add ButtonComponent and AlertComponent in entryComponent list. In ngDoBootstrap and define them as custom elements. This is how my app.module looks:
runtime, polyfills, script
js files into single script file and exportelements.js
which contains the custom elements (optional: gzip those files) serve it using http-server deploy --gzipscript.js
andelements.js
in index.html (in deploy directory) to tell the browser about the custom elements. Now my-button and my-alert can be included in index.html . In this example, the button is shown on-load and Alert message is added dynamically (with counter number) on click of the button. Here is the code:Here is my link for my git repo
Hope this will help!
Thanks.