目前我正在开发一个托管在客户端服务器上的项目。对于新的“模块”有无意重新编译整个应用程序。也就是说,客户想要在运行时更新路由器/延迟加载模块。我已经尝试了几种方法,但无法使其发挥作用。我想知道你们中是否有人知道我还可以尝试什么或我错过了什么。
我注意到的一件事是,在构建应用程序时,我使用 Angular cli 尝试过的大多数资源默认被 webpack 捆绑成单独的块。这看起来很合乎逻辑,因为它利用了 webpack 代码分割。但是如果模块在编译时尚不可知(但编译后的模块存储在服务器上的某个位置)怎么办?捆绑不起作用,因为找不到要导入的模块。只要在系统上找到 UMD 模块,使用 SystemJS 就会加载它们,但也会被 webpack 捆绑在单独的块中。
我已经尝试过一些资源;
- 动态远程组件加载器
- 模块加载
- 在运行时从不同服务器加载模块
- 如何将动态外部组件加载到 Angular 应用程序中
- 在 Angular 2、4、5、6 中实现插件架构/插件系统/可插拔框架
- Angular 5 - 在运行时动态加载模块(在编译时未知)
- https://medium.com/@nikolasleblanc/building-an-angular-4-component-library-with-the-angular-cli-and-ng-packagr-53b2ade0701e
- 其他一些与此主题相关的内容。
我已经尝试并实现了一些代码,但目前不起作用;
使用普通 module.ts 文件扩展路由器
this.router.config.push({
path: "external",
loadChildren: () =>
System.import("./module/external.module").then(
module => module["ExternalModule"],
() => {
throw { loadChunkError: true };
}
)
});
UMD 包的正常 SystemJS 导入
System.import("./external/bundles/external.umd.js").then(modules => {
console.log(modules);
this.compiler.compileModuleAndAllComponentsAsync(modules['External'])
.then(compiled => {
const m = compiled.ngModuleFactory.create(this.injector);
const factory = compiled.componentFactories[0];
const cmp = factory.create(this.injector, [], null, m);
});
});
导入外部模块,不使用 webpack (afaik)
const url = 'https://gist.githubusercontent.com/dianadujing/a7bbbf191349182e1d459286dba0282f/raw/c23281f8c5fabb10ab9d144489316919e4233d11/app.module.ts';
const importer = (url:any) => Observable.fromPromise(System.import(url));
console.log('importer:', importer);
importer(url)
.subscribe((modules) => {
console.log('modules:', modules, modules['AppModule']);
this.cfr = this.compiler
.compileModuleAndAllComponentsSync(modules['AppModule']);
console.log(this.cfr,',', this.cfr.componentFactories[0]);
this.external.createComponent(this.cfr.componentFactories[0], 0);
});
使用 SystemJsNgModuleLoader
this.loader.load('app/lazy/lazy.module#LazyModule')
.then((moduleFactory: NgModuleFactory<any>) => {
console.log(moduleFactory);
const entryComponent = (<any>moduleFactory.moduleType).entry;
const moduleRef = moduleFactory.create(this.injector);
const compFactory = moduleRef.componentFactoryResolver
.resolveComponentFactory(entryComponent);
});
尝试加载用 rollup 制作的模块
this.http.get(`./myplugin/${metadataFileName}`)
.map(res => res.json())
.map((metadata: PluginMetadata) => {
// create the element to load in the module and factories
const script = document.createElement('script');
script.src = `./myplugin/${factoryFileName}`;
script.onload = () => {
//rollup builds the bundle so it's attached to the window
//object when loaded in
const moduleFactory: NgModuleFactory<any> =
window[metadata.name][metadata.moduleName + factorySuffix];
const moduleRef = moduleFactory.create(this.injector);
//use the entry point token to grab the component type that
//we should be rendering
const compType = moduleRef.injector.get(pluginEntryPointToken);
const compFactory = moduleRef.componentFactoryResolver
.resolveComponentFactory(compType);
// Works perfectly in debug, but when building for production it
// returns an error 'cannot find name Component of undefined'
// Not getting it to work with the router module.
}
document.head.appendChild(script);
}).subscribe();
仅当模块已在应用程序的 RouterModule 中作为“惰性”路由提供时,SystemJsNgModuleLoader 的示例才有效(使用 webpack 构建时会将其转换为块)
我在 StackOverflow 上发现了很多关于这个主题的讨论,并且提供的解决方案似乎非常适合动态加载模块/组件(如果预先知道的话)。但没有一个适合我们的项目用例。请让我知道我仍然可以尝试或深入研究什么。
Thanks!
编辑:我发现;https://github.com/kirjs/angular-dynamic-module-loading并会尝试一下。
更新:我创建了一个存储库,其中包含使用 SystemJS(并使用 Angular 6)动态加载模块的示例;https://github.com/lmeijdam/angular-umd-dynamic-example