首页 > 解决方案 > 组件不是已知元素

问题描述

我有一个位于模块中的shared组件,并且我在另一个组件中成功使用了该组件,但是,当我尝试在第二个组件中使用它时,我看到以下错误;

ERROR in src/app/dashboard/dashboard-report/dashboard-report.component.html:1:1 - error NG8001: 'app-header-unauthenticated' is not a known element:
1. If 'app-header-unauthenticated' is an Angular component, then verify that it is part of this module.
2. If 'app-header-unauthenticated' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

我已经查看了我所做的事情,但我看不出我哪里出错了。

我有一个名为的模块shared,它有一个名为的组件header-unauthenticated,它被导出,因此它可以在其他模块/组件中使用。这是shared模块;

@NgModule({
  declarations: [
    AlertComponent,
    HeaderUnauthenticatedComponent
  ],
  exports: [
    AlertComponent,
    HeaderUnauthenticatedComponent
  ],
  imports: [
    CommonModule
  ]
})
export class SharedModule { }

我正在尝试在另一个模块header-unauthenticated内的一个名为的组件内使用该组件。我已经将模块导入到模块中,以便可以使用它的组件,但它们似乎不起作用。这是模块;dashboard-reportdashboardshareddashboarddashboard

@NgModule({
  declarations: [DashboardReportComponent],
  imports: [
    CommonModule,
    SharedModule
  ]
})
export class DashboardModule { }

我里面有另一个模块access和一个组件login,我可以header-unauthenticated毫无问题地使用该组件,并且将access模块与dashboard模块进行比较,我看不出任何差异。

@NgModule({
  declarations: [LoginComponent],
  imports: [
    CommonModule,
    SharedModule,
    ReactiveFormsModule
  ]
})
export class AccessModule { }

我哪里出错了,为什么header-unauthenticated找不到组件?

标签: angular

解决方案


发现问题,dashboard组件没有被导入app.module.ts文件,添加解决了问题;

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    AngularFireModule.initializeApp(environment.firebase),
    AppRoutingModule,
    AccessModule,
    DashboardModule,
    SharedModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

推荐阅读