问题描述
我有三个组件( NavbarComponent , IndexComponent , PoweredComponent ),所以我使用延迟加载,我需要在多个组件中使用组件,不知道如何在惰性模块中声明要使用的组件。我将向您展示不同文件的一些相关部分:
- NavbarComponent
navbar.module.ts
declarations: [NavbarComponent],imports: [
CommonModule
],exports: [NavbarComponent]
})
- IndexComponent
Index.module.ts
declarations: [IndexComponent,NavbarComponent],imports: [
CommonModule,IndexRoutingModule,],})
index.component.html
<app-navbar></app-navbar>
<div>...</div>
在索引中显示导航栏
- PoweredComponent
powered.module.ts
@NgModule({
declarations: [PoweredComponent,PoweredRoutingModule,})
powered.component.html
<app-navbar></app-navbar>
<div>...</div>
在powered.component.html
中没有显示导航栏
请帮助我???
谢谢阅读!!!
解决方法
您不能在多个NgModule中声明一个组件(使用declarations
)。
在您的情况下,NavbarModule
导出NavBarComponent
时,您只需要在PoweredModule
和IndexModule
中导入模块,即可在这些模块中使用该组件。>
以IndexModule
为例:
declarations: [IndexComponent],imports: [
CommonModule,IndexRoutingModule,NavbarModule
],})
,
Index.module.ts
declarations: [IndexComponent],})
powered.module.ts
@NgModule({
declarations: [PoweredComponent],PoweredRoutingModule,})