给定三个使用 v6.1.9 的 @angular 项目:host、alpha和beta
alpha并beta使用 @angular/elements 创建和定义一个 Web 组件,如下所示:
constructor(private injector: Injector) {}
ngDoBootstrap() {
const config: NgElementConfig = { injector: this.injector };
const component= createCustomElement(component, config);
customElements.define("alpha-component", component); // beta-component respectively
}
alpha并beta使用构建ng build --prod --output-hashing none,然后运行构建后脚本以按以下顺序连接生成的文件:scripts.js, styles.js, runtime.js, main.js.
polyfills.js 被跳过,因为它main.ts会在加载库时检查使用的 polyfills 是否已经定义(例如,避免尝试重新定义 zone.js)。
结果束是alpha-component.bundle.js和beta-component.bundle.js。
host使用标签引用上述捆绑包<head>。index.html<script defer>
alpha如果捆绑包是按then的顺序引用的beta,我会看到alpha尝试引导两次;以相反的顺序,我会看到beta尝试引导两次。
由于第一个引用的捆绑包尝试引导两次,它尝试为捆绑包定义 Web 组件两次,从而导致错误,并且永远不会注册第二个引用的捆绑包的 Web 组件。
目标是能够使用 @angular 创建许多 Web 组件,然后在其他 @angular 或insert framework here技术中使用它们。