【发布时间】:2018-10-23 10:51:36
【问题描述】:
给定三个使用 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 将检查在加载库时是否已经定义了所使用的 polyfill(例如,避免尝试重新定义 zone.js)。
生成的包是alpha-component.bundle.js 和beta-component.bundle.js。
host 使用<script defer> 标签在index.html 的<head> 中引用上述捆绑包。
如果捆绑包的引用顺序是alpha 然后beta,我会看到alpha 尝试引导两次;以相反的顺序,我会看到beta 尝试引导两次。
由于第一个引用的包尝试引导两次,它尝试为该包定义 Web 组件两次,导致错误,并且从未注册第二个引用的包的 Web 组件。
目标是能够使用 @angular 创建许多 Web 组件,然后在其他 @angular 或 insert framework here 技术中使用它们。
【问题讨论】:
-
你可能还会觉得这个问题很有趣stackoverflow.com/a/53836552/1085483
标签: angular web-component angular-elements