Angular 如何访问父模块中声明的组件
角度组件无法识别模块中声明的组件 它是在年宣布的。 我可以在ChildComponent中使用CommentsComponent,但不能在子模块中使用。我尝试创建一个共享模块,该模块被导入父模块,然后导出到子模块。然而,这给了我一个Firebase错误。当我试图直接在ChildModule中声明CommentsComponent时,会出现以下错误: CommentsComponent是两个模块声明的一部分:TabsModule和DocDetailModule 但是,当我仅在父模块中声明它或直接在子模块中声明它时,它无法识别该组件 我尝试了许多不同的方法,但没有成功 父模块:Angular 如何访问父模块中声明的组件,angular,Angular,角度组件无法识别模块中声明的组件 它是在年宣布的。 我可以在ChildComponent中使用CommentsComponent,但不能在子模块中使用。我尝试创建一个共享模块,该模块被导入父模块,然后导出到子模块。然而,这给了我一个Firebase错误。当我试图直接在ChildModule中声明CommentsComponent时,会出现以下错误: CommentsComponent是两个模块声明的一部分:TabsModule和DocDetailModule 但是,当我仅在父模块中声明它或直接在
imports:[childModule // page component with module and routing]
declarations: [
...
ChildComponent //no module
CommentsComponent,
...
],`
在模块中声明CommentsComponent并将其导出到其他模块:
//Comments.module.ts:(模块正在共享)
//需要组件的子模块:
@NgModule({
imports: [
CommonModule,
...
],
declarations: [...],
entryComponents: [...],
})
@NgModule({
imports: [
...
CommentsModule,
DocDetailModule,]
//封装子模块和子组件的父模块:
@NgModule({
imports: [
CommonModule,
...
],
declarations: [...],
entryComponents: [...],
})
@NgModule({
imports: [
...
CommentsModule,
DocDetailModule,]
//该组件可处理firebase错误(((
如果您想使用一个将在多个模块中使用的组件,那么只需创建一个
共享
模块,然后在您想使用该通用
组件的每个位置使用该共享
模块,并生成注释组件
目录结构
shared(module)
components
commentsComponent
comments.component.ts
comments.component.html
share.module.ts
在shared.module.ts的内部执行此操作
declarations: [CommentsComponent]
exports: [commentsComponent]
在导出
数组中包含您的共享
组件,以便您可以访问任何模块
现在,当您想使用注释.component
时,让我们在module1
中这样做,只需在module1.module.ts
import { SharedModule } from 'path-to-shared-module';
imports : [SharedModule]
通过这种方式,您可以在应用程序的多个模块中使用共享组件。首先,感谢您的回复。如果在父模块中声明了子组件,并且将子模块导入父模块,则子模块是否能够访问子组件?