简体   繁体   English

在多组件角度中使用组件延迟加载?

[英]Use component lazy loading in multi components angular?

I have Three components ( NavbarComponent , IndexComponent , PoweredComponent ) so I use lazy loading I need use components in multi components ,I don't know how to declare the component for using it in the lazy module.我有三个组件( NavbarComponentIndexComponentPoweredComponent )所以我使用延迟加载我需要在多个组件中使用组件,我不知道如何声明组件以在惰性模块中使用它。 I'll show you some of the relevant parts of the different files:我将向您展示不同文件的一些相关部分:

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

index.component.html index.component.html

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

in index show navbar在索引中显示导航栏

index.component.html

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

powered.component.html power.component.html

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

in powered.component.html no show navbarpowered.component.html没有显示导航栏

issue <app-navbar></app-navbar> not dispaly in multi components can show jsut in index components issue <app-navbar></app-navbar> not dispaly in multi components can show jsut in index components

Pleas help me???请帮帮我???
Thank's to read!!!感谢阅读!!!

You can't declare a Component (using declarations ) in more than one NgModule.你不能在多个 NgModule 中声明一个组件(使用declarations )。 In your situation, as your NavbarModule exports the NavBarComponent , you just need to import the module in the PoweredModule and IndexModule to make the Component available in these modules.在您的情况下,当您的NavbarModule导出NavBarComponent ,您只需要在PoweredModuleIndexModule导入模块即可使 Component 在这些模块中可用。

So for IndexModule as an example :所以以IndexModule为例:

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

Index.module.ts索引.module.ts

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

powered.module.ts power.module.ts

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM