SystemJsNgModuleLoader
,但无法使其工作:
this.loader.load(url).then(console.info);
我得到无法找到模块xxx的任何字符串我用于URL(aboslute /相对urls /路径…尝试了许多选项).我看过路由器的源代码,找不到任何其他的,然后这个SystemJsNgModuleLoader.我甚至不确定我应该使用这个…
昨天在ng-europe 2016年会议上,Miško&马蒂亚斯回答说:
Miško Hevery:
One just has to get hold of the module,from there you can get the hold of component factory and you can dynamically load component factory anywhere you want in the application. This is exactly what the router does internally. So it’s quite strait forward for you to do that as well.Matias Niemelä
The only special thing to note is that on the [Ng]Module there’s something calledentryComponents
and that identifies components that could be lazy loaded – that’s the entry into that component set. So when you have modules that are lazy loaded,please put the stuff intoentryComponents
.
…但不是没有例子的海峡前进,
任何人都知道如何手动加载模块,而不使用Route.loadChildren?如何掌握模块,什么是应该进入entryComponents的东西(我读了FAQ,但不能尝试没有实际加载模块)?
Anyone knows how to load modules manually,without using
Route.loadChildren?
它可能看起来像这样:
this.loader.load('./src/test.module#TestModule').then((factory: NgModuleFactory<any>) => { console.log(factory); });
TestModule位于./src/test.module.ts文件(绝对路径)
这也是更高级的例子
> https://github.com/angular/angular/issues/11738
如果您不加载组件声明如下:
<my-comp></my-comp>
在模板中,如果要使用此组件进行动态加载,则必须通过以下方法之一在entryComponent中引发此组件:
> @NgModule中的entryComponents数组
> @Component中的entryComponents数组
> ANALYZE_FOR_ENTRY_COMPONENTS(角度路由器使用此方法https://github.com/angular/angular/blob/2.1.2/modules/%40angular/router/src/router_module.ts#L198)
编译器创建这样的工厂:
对于每个entryComponent – host.factory.js
对于每个指令 – wrapper.ngfactory.js(对于版本〜2)
对于每个组件component.ngfactory.js
也可以看看