Angular 5 - 将模块导入动态模块

时间:2018-05-18 16:38:57

标签: javascript typescript angular5 owl-carousel

我对Angular 5比较陌生。我一直在尝试使用从服务中检索的模板来创建动态组件。我已经能够使用此功能渲染基本模板:

createComponent(): void {
  this.container.clear();
  // create dynamic component using template and item
  const component = Component({template: this.template})(class {});
  const module = NgModule({
    imports: [
      CommonModule
    ],
    declarations: [component],
    entryComponents: [component]
  })(class {});
  // compile module
  this.compiler.compileModuleAndAllComponentsAsync(module)
    .then((factories) => {
      const factory = factories.componentFactories[0];        
      this.componentRef = this.container.createComponent(factory);
      this.componentRef.instance.item = this.item;
      this.componentRef.instance.app = this.app;
    });
}

现在我有简单的模板渲染,我试图在我的模板中获得一个猫头鹰轮播渲染。当我尝试将Angular模块OwlModule导入我的动态NgModule时,没有任何内容呈现,我也没有在控制台中收到任何错误。

以下是我在模板中用于呈现轮播的代码:

<owl-carousel *ngIf="trendingItems.length > 0" [items]="trendingItems[0]['Carousel Media']" [carouselClasses]="['owl-theme', 'row', 'sliding']">
  <div *ngFor="let media of trendingItems[0]['Carousel Media']">
    <div *ngIf="media.MediaTypeId == 1000 || media.MediaTypeId == 1001">
      <div layout="column" layout-fill="">
        <div *ngIf="media.MediaTypeId == 1000" class="img-container">
          <img data-src="{{app.mediaPath(media)}}" class="img-responsive" id="image-height">
        </div>
        <div *ngIf="media.MediaTypeId == 1001" class="videoBox">
          <video media="media"></video>
        </div>
      </div>
    </div>
  </div>
</owl-carousel>

当我将其硬编码为非动态模板时,这很好,所以我不相信这是问题所在。此外,即使页面上没有owl-carousel指令,模板仍然无法呈现。

我有什么遗失的东西吗?有没有一种特殊的方法可以将自定义模块导入动态模块?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

我相信Webpack的动态导入功能可以支持这一点:

https://webpack.js.org/guides/code-splitting/

相关问题