gpt4 book ai didi

angular - 在 angular 项目中使用多个 angular 元素作为 web 组件

转载 作者:行者123 更新时间:2023-12-01 13:17:54 28 4
gpt4 key购买 nike

鉴于三个@angular 项目都使用 v6.1.9:host , alpha , 和 betaalphabeta创建和定义一个 web 组件,每个组件都使用 @angular/elements ,如下所示:

constructor(private injector: Injector) {}

ngDoBootstrap() {
const config: NgElementConfig = { injector: this.injector };
const component= createCustomElement(component, config);
customElements.define("alpha-component", component); // beta-component respectively
}
alphabeta使用 ng build --prod --output-hashing none 构建然后运行后构建脚本以按以下顺序连接生成的文件: scripts.js, styles.js, runtime.js, main.js .

polyfills.js 被跳过,因为 main.ts将在加载库时检查所使用的 polyfill 是否已经定义(例如,避免尝试重新定义 zone.js)。

得到的包是 alpha-component.bundle.jsbeta-component.bundle.js .
host<head> 中引用了上述包的 index.html<script defer>标签。

如果以 alpha 的顺序引用包然后 beta ,我会看到 alpha尝试引导两次;在相反的顺序,我会看到 beta尝试引导两次。

由于第一个引用的 bundle 尝试引导两次,它尝试为 bundle 定义 web 组件两次,导致错误,并且永远不会注册第二个引用的 bundle 的 web 组件。

目标是能够使用@angular 创建许多Web 组件,然后在其他@angular 或 insert framework here 中使用它们。技术。

最佳答案

不幸的是,bundle 的连接在这里不起作用,b/c webpack 使用的是全局变量。由 alpha 包创建的变量将被 beta 包的变量覆盖。

你可以在包中重命名这个变量,或者你可以使用 [1] 并且它是 --single-bundle 开关。

首先,忘记自述文件关于外部的内容。这是进一步的优化技术,其中主机、alpha 和 beta 可以共享相同的库。这可以防止您多次加载 Angular。

也许,还有我的blog series about Angular Elements对你来说很有趣[2]。

最好的祝愿,
曼弗雷德

[1] https://github.com/manfredsteyer/ngx-build-plus

[2] https://www.softwarearchitekt.at/post/2018/07/13/angular-elements-part-i-a-dynamic-dashboard-in-four-steps-with-web-components.aspx

关于angular - 在 angular 项目中使用多个 angular 元素作为 web 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52947217/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com