Angular 组件选择器不是已知元素
我有两个模块Angular 组件选择器不是已知元素,angular,angular7,Angular,Angular7,我有两个模块AppModule和MyArtModule,其中声明了一些组件 当我尝试在GraphicsComponent中使用ConfirmComponent选择器时,它工作正常,但当我尝试在MyArtComponent中使用ConfirmComponent选择器时,它抛出了一个错误“artifi-confirm-alert”不是已知元素: “人工确认警报”不是已知元素: 如果'artifi confirm alert'是一个角度组件,则验证它是否是该模块的一部分 如果'artifi confi
AppModule
和MyArtModule
,其中声明了一些组件
当我尝试在GraphicsComponent
中使用ConfirmComponent
选择器时,它工作正常,但当我尝试在MyArtComponent
中使用ConfirmComponent
选择器时,它抛出了一个错误“artifi-confirm-alert”不是已知元素:
“人工确认警报”不是已知元素:
- 应用模块
- 图形组件
- 确认组件
- MyArt模块
- MyArtComponent
应用模块代码
@NgModule({
declarations: [
GraphicsComponent,
ConfirmAlertComponent
],
imports: [
BrowserModule,
MyArtModule
],
...
})
@NgModule({
declarations: [
MyArtComponent,
],
imports: [
CommonModule
],
providers: [
MyArtService
],
exports: [
MyArtComponent
]
})
MyArtModule代码
@NgModule({
declarations: [
GraphicsComponent,
ConfirmAlertComponent
],
imports: [
BrowserModule,
MyArtModule
],
...
})
@NgModule({
declarations: [
MyArtComponent,
],
imports: [
CommonModule
],
providers: [
MyArtService
],
exports: [
MyArtComponent
]
})
复制的问题代码- 我用
OneComponent
创建了OneModule
,用twomcomponent
创建了TwoModule
可以在AppComponent中访问
和
但无法访问AppOne组件中的
。您的ConfirmComponent和GraphicsComponent位于同一模块中!所以他们认识对方!
但是您试图将ConfirmComponent用于另一个模块中的另一个组件(在您的示例中为MyArtModule),而该模块不知道该组件是否存在
您必须导出ConfirmComponent,使其能够在整个应用程序中使用
在AppModule的导出数组中添加ConfirmComponent
exports: [
ConfirmComponent
]
您正在尝试访问子模块组件中的父模块组件。创建共享模块并在其中添加组件
@NgModule({
declarations: [ConfirmComponent],
exports: [ConfirmComponent],
})
export class SharedModule {
}
并将其添加到AppModule中:
@NgModule({
declarations: [
GraphicsComponent,
],
imports: [
BrowserModule,
MyArtModule,
SharedModule
],
...
})
并将其导入您的艺术模块:
@NgModule({
imports: [
CommonModule,
SharedModule
],
declarations: [ConfirmAlertComponent],
exports: [
ConfirmAlertComponent
]
})
export class MyArtModule { }
在AppModule的导出数组中添加了ConfirmComponent,但仍然存在相同的错误。能否发送confirm-component.ts文件中的内容?没有,只是一个新组件。请检查问题我添加了一个复制问题的链接。我为确认组件创建了一个新模块,并导入了应用程序模块,但仍然收到相同的错误,尝试在stackbliz中复制链接有问题。