【问题标题】:Use component lazy loading in multi components angular?在多组件角度使用组件延迟加载?
【发布时间】:2020-12-29 07:50:48
【问题描述】:

我有三个组件(NavbarComponent、IndexComponent、PoweredComponent)所以我使用延迟加载我需要在多组件中使用组件,我不不知道如何声明组件以在惰性模块中使用它。我将向您展示不同文件的一些相关部分:

  1. 导航栏组件
    navbar.module.ts
  declarations: [NavbarComponent],
  imports: [
    CommonModule
  ],
  exports: [NavbarComponent]
})
  1. 索引组件
    Index.module.ts
  declarations: [IndexComponent ,NavbarComponent],
  imports: [
    CommonModule,
    IndexRoutingModule,
  ],
})

index.component.html

<app-navbar></app-navbar>
<div>...</div>

在索引显示导航栏

  1. 供电组件
    powered.module.ts
@NgModule({
  declarations: [PoweredComponent,NavbarComponent],
  imports: [
    CommonModule,
    PoweredRoutingModule,
  ], 
})

powered.component.html

<app-navbar></app-navbar>
<div>...</div>

在powered.component.html没有显示导航栏

issue &lt;app-navbar&gt;&lt;/app-navbar&gt; not dispaly in multi components can show jsut in index components

请帮帮我???
感谢阅读!!!

【问题讨论】:

    标签: angular angular-components angular-module


    【解决方案1】:

    Index.module.ts

      declarations: [IndexComponent],
      imports: [
        CommonModule,
        IndexRoutingModule,
        NavbarModule
      ],
    })
    

    powered.module.ts

    @NgModule({
      declarations: [PoweredComponent],
      imports: [
        CommonModule,
        PoweredRoutingModule,
        NavbarModule
      ], 
    })
    

    【讨论】:

      【解决方案2】:

      你不能在多个 NgModule 中声明一个组件(使用declarations)。 在您的情况下,当您的NavbarModule 导出NavBarComponent 时,您只需在PoweredModule 和IndexModule 中导入模块,以使组件在这些模块中可用。

      所以以IndexModule为例:

       declarations: [IndexComponent],
        imports: [
          CommonModule,
          IndexRoutingModule,
          NavbarModule
        ],
      })
      

      【讨论】:

        猜你喜欢
        • 2015-07-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-02
        • 2020-10-17
        • 1970-01-01
        • 1970-01-01
        • 2020-07-24
        • 1970-01-01
        相关资源
        最近更新 更多