Angular 4 ngComponentOutlet和延迟加载模块

时间:2017-07-11 10:34:57

标签: angular lazy-loading

我现在开始得到角4的悬念,特别是动态组件部分。

我设法成功复制了描述为here ngComponentOutlet 方法,plunkr here

现在我想做的是延迟加载包含动态组件的模块。我看到on Github这个功能应该已经实现,但是我没有足够的角度4来解释源变化本身,我无法在任何地方找到这种方法的一个例子。

另外,我想在不依赖a4路由器和webpack的情况下实现这种延迟加载(如果重要的话)。

有人愿意帮忙吗?

TIA

1 个答案:

答案 0 :(得分:4)

您可以使用编译器的compileModuleAndAllComponentsAsync方法。这些方面的东西:

ngAfterViewInit() {
  System.import('app/t.module').then((module) => {
      _compiler.compileModuleAndAllComponentsAsync(module.TModule)
        .then((compiled) => {
          const m = compiled.ngModuleFactory.create(this._injector);
          const factory = compiled.componentFactories[0];
          const cmp = factory.create(this._injector, [], null, m);
        })
    })
}

有关详细信息,请参阅Here is what you need to know about dynamic components in Angular

相关问题