customElements.define('my-element', MyElement);
MDN Docs
@angular/elements
CustomElementRegistry
// No polyfill; limited browser support
npm install @angular/elements --save-dev
// Polyfill included
ng add @angular/elements
{
// ...
"compilerOptions": {
// ...
"target": "es2015"
}
}
bootstrap
propertyngDoBootstrap()
methodentryComponents
propertycreateCustomElement
from elements
constructor(private injector: Injector) {
const el = createCustomElement(TitleComponent,
{ injector: this.injector }
);
customElements.define('custom-title', el);
// ...
}
angular-cli
does not have build for elements
// Prod build, disable hashing in output
ng build --prod --output-hashing none
// Concat the scripts into single .js file
cat runtime* polyfills* main* scripts* > elements.js
<!DOCTYPE html>
<html lang="en">
<head>
<!-- ... -->
<script src="./elements.js"></script>
</head>
<body>
<!-- ... -->
<my-custom-element></my-custom-element>
<!-- ... -->
</body>
</html>
// Outputs as custom events
element = document.getElementById('id');
if (element) {
element.addEventListener('outputName', (item: any) =>
// some logic
);
}
// REACT: App.tsx
class App extends Component {
render() {
return (
);
}
}
angular-cli
doesn't yet have build path for it
@angular/elements
to export components as custom
elements. Both official and unofficial packages.
angular-cli
support (hopefully)