Angular 5 : Lazy-loading of component without routing

标签 angular angular5 lazy-loading

在我的网络应用程序中,我有一个“条款和条件”弹出窗口,通过单击页脚中的链接打开(因此它是一个核心组件)。 弹出窗口由几个选项卡组成,每个选项卡都是一个相当大的模板文件。

我想知道是否可以将选项卡模板移动到单独的 block 并组织它们的延迟加载?我不确定我是否可以接受默认的 Angular 延迟加载,因为我不想为弹出窗口设置单独的路由。

最佳答案

您可以等待用户点击链接,一旦点击事件发生,就在 View 中加载所需的组件。 注意事项:

  1. 您需要为 View 中的组件定义一个占位符。
  2. 条款和条件组件需要定义为入门级组件,因为它是模块或使用它的模块。

     entryComponents: [
        ChildComponent
      ],
    
  3. 确保引用组件中的占位符并动态附加条款和条件组件。

      <div>
          <ng-template #viewContainerRef></ng-template>
       </div>
    

@ViewChild('viewContainerRef', { read: ViewContainerRef }) VCR: ViewContainerRef;

并动态创建组件:

createComponent() {

    let componentFactory = this.CFR.resolveComponentFactory(ChildComponent);
    let componentRef: ComponentRef<ChildComponent> = this.VCR.createComponent(componentFactory);
    let currentComponent = componentRef.instance;

    currentComponent.selfRef = currentComponent;
  // to track the added component, you can reuse this index to remove it later
    currentComponent.index = ++this.index; 

    // providing parent Component reference to get access to parent class methods
    currentComponent.compInteraction = this;

}

这里有一个例子:https://add-or-remove-dynamic-component-parent-child.stackblitz.io

关于 Angular 5 : Lazy-loading of component without routing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50648142/

相关文章:

angular - 将项目从 Angular 5 迁移到 8 问题与 Response -> HttpResponse

html - 增加 IcoFont 的高度

javascript - 错误后可观察到 rx 损坏

javascript - native 延迟加载图像回流(加载 ='"延迟”)

java - hibernate 计数集合大小而不初始化

Angular:如何确定带参数的事件路线?

Angular 5 创建一个服务的多个实例

angular5 - Angular 5 动态变量名

typescript - 从 MS Graph API 获取用户照片并显示图像

javascript - 如何将 lazyload 和 bxslider 集成在一起?