首页 > 解决方案 > 从构建中排除一些惰性模块(Angular5)

问题描述

我正在开发一个项目,该项目在多个客户中运行。有许多延迟加载的模块,其中大部分被所有客户使用。但是有些模块只在少数地方需要,所以我想将它们从其他地方的构建中排除。

可能吗?

标签: angulartypescript

解决方案


像这样的东西怎么样:(然后您可以使用 if 语句等来加载不同的模块)

export class AppComponent implements AfterViewInit {

  @ViewChild('testOutlet', {read: ViewContainerRef}) testOutlet: ViewContainerRef;
  constructor(
    private loader: NgModuleFactoryLoader,
    private injector: Injector) {
    }

    ngAfterViewInit(): void {
    const path = 'src/app/lazy/lazy.module#LazyModule';
    this.loader.load(path).then((moduleFactory: NgModuleFactory<any>) => {
      const entryComponent = (<any>moduleFactory.moduleType).entry;
      const moduleRef = moduleFactory.create(this.injector);

      const compFactory = moduleRef.componentFactoryResolver.resolveComponentFactory(entryComponent);
      this.testOutlet.createComponent(compFactory);
  });
  }
}

推荐阅读