I have to build a web component to inject inside a legacy code project; I tried to follow the official documentation to do so using angular (https://v19.angular.dev/guide/elements) but I feel like is a bit incomplete: for example there is nothing about how do produce a js file to then refer in the html of the target project to be able to use the custom html tag.
I searched extensively for guides or more about the topic but nothing pops out on the Internet, at least not for the newer versions of Angular (newest stuff I found was 2 year old).
I followed the official guide 'till the end and tried to ng build and use the main.js and polyfill.js inside of an html file to reference the custom html tag created in Angular
but I get the following error that is not so explanatory:
main-ZVR3556L.js:7 y: NG0908
at new e (file:///C:/Users/sviaga/Desktop/testProjects/dist/main-ZVR3556L.js:7:19677)
at Object.ngZoneFactory [as useFactory] (file:///C:/Users/sviaga/Desktop/testProjects/dist/main-ZVR3556L.js:7:59858)
at Object.r [as factory] (file:///C:/Users/sviaga/Desktop/testProjects/dist/main-ZVR3556L.js:7:5002)
at Ke.hydrate (file:///C:/Users/sviaga/Desktop/testProjects/dist/main-ZVR3556L.js:7:4140)
at Ke.get (file:///C:/Users/sviaga/Desktop/testProjects/dist/main-ZVR3556L.js:7:3304)
at vf (file:///C:/Users/sviaga/Desktop/testProjects/dist/main-ZVR3556L.js:7:63982)
at Ca (file:///C:/Users/sviaga/Desktop/testProjects/dist/main-ZVR3556L.js:7:65759)
at lo (file:///C:/Users/sviaga/Desktop/testProjects/dist/main-ZVR3556L.js:7:77258)
at file:///C:/Users/sviaga/Desktop/testProjects/dist/main-ZVR3556L.js:7:82993
Anyone out there building web components in angular that's facing my same troubles?
This is my app.component.ts:
import { Component, Injector } from '@angular/core';
import { createCustomElement } from '@angular/elements';
import { NotificationsCenterComponent } from './notifications-center/notifications-center.component';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrl: './app.component.scss',
})
export class AppComponent {
constructor(injector: Injector) {
const NotificationsCenterElement = createCustomElement(NotificationsCenterComponent, { injector });
customElements.define('notifications-center', NotificationsCenterElement);
}
}
This is my notifications-center.ts and html;
import { Component } from '@angular/core';
@Component({
selector: 'app-notifications-center',
imports: [],
templateUrl: './notifications-center.component.html',
styleUrl: './notifications-center.component.scss'
})
export class NotificationsCenterComponent {
}
<p>notifications-center works!</p>
As you can tell the component is default just because I wanted to test stuff before actually starting the developing.
This is the html file where I'm trying to use the web-component:
<html>
<head>
<script src="dist/main-ZVR3556L.js"></script>
<script src="dist/polyfills-B6TNHZQ6.js"></script>
</head>
<body>
<notifications-center></notifications-center>
</body>
</html>