gpt4 book ai didi

angular - 从另一个 url-angular4 加载动态模块

转载 作者:太空狗 更新时间:2023-10-29 17:20:49 26 4
gpt4 key购买 nike

是否可以引用一个模块(已经以 umd 或 es 格式编译)并在已编译的 Angular 应用程序中动态加载它?

  1. 主 shell 应用程序托管于:http://plugin_shell.mydomain.com
  2. 一个模块(带有一堆组件、服务等):编译后的代码托管在另一个网址。让我们说: http://plugins/modules/myfirstplugin.umd.js
  3. 当外壳加载时。加载所有模块,渲染特定组件,引用服务,使用类等

我尝试使用 SystemJsNgModuleLoader.load 加载模块,但它似乎确实适用于此类用例。

谢谢

编辑:同样的问题(没有答案):How to dynamically load external angular 2 module (like served from an external module.bundle.js)

最佳答案

你可以这样做:

@Component({
providers: [
{
provide: NgModuleFactoryLoader,
useClass: SystemJsNgModuleLoader
}
]
})
export class ModuleLoaderComponent {
constructor(private _injector: Injector,
private loader: NgModuleFactoryLoader) {
}

ngAfterViewInit() {
this.loader.load('app/t.module#TModule').then((factory) => {
const module = factory.create(this._injector);
const r = module.componentFactoryResolver;
const cmpFactory = r.resolveComponentFactory(AComponent);

// create a component and attach it to the view
const componentRef = cmpFactory.create(this._injector);
this.container.insert(componentRef.hostView);
})
}
}

阅读Here is what you need to know about dynamic components in Angular更多细节。特别是动态模块加载和编译部分。

关于angular - 从另一个 url-angular4 加载动态模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45571283/

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