是否可以引用模块(已以 umd 或 es 格式编译)并将其动态加载到已编译的角度应用程序中?
- 主 shell 应用程序托管于:http://plugin_shell.mydomain.com http://plugin_shell.mydomain.com
- 模块(带有一堆组件、服务等):编译后的代码
托管在另一个网址。让我们说:http://plugins/modules/myfirstplugin.umd.js http://plugins/modules/myfirstplugin.umd.js
- 当外壳加载时。加载所有模块,渲染具体组件,参考
服务、使用等级等。
我尝试使用 SystemJsNgModuleLoader.load 加载模块,但它似乎确实适用于此类用例。
Thanks
编辑:同样的问题(没有答案):如何动态加载外部 Angular 2 模块(例如从外部 module.bundle.js 提供服务) https://stackoverflow.com/questions/42401013/how-to-dynamically-load-external-angular-2-module-like-served-from-an-external
你可以这样做:
@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);
})
}
}
Read 以下是您需要了解的有关 Angular 动态组件的知识 https://blog.angularindepth.com/here-is-what-you-need-to-know-about-dynamic-components-in-angular-ac1e96167f9e更多细节。具体来说Dynamic module loading and compilation
部分。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)