【问题标题】:Regarding header footer in another module in angular关于角度另一个模块中的页眉页脚
【发布时间】:2022-01-18 15:13:24
【问题描述】:

我是 Angular 的新手。我创建了页眉、页脚和侧边栏。如果我在这个 dishoard 组件中使用它,我正在创建仪表板组件。所以它在组件中工作。然后我为员工创建了一个模块。如果我在这个员工模块中使用页眉页脚边栏意味着它的显示错误

  1. 如果 'app-header' 是一个 Angular 组件,则验证它是该模块的一部分。
  2. 如果“app-header”是 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以禁止显示此消息。

1


src/app/employee/list-employee/list-employee.component.ts:5:16
  5   templateUrl: './list-employee.component.html',
                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Error occurs in the template of component ListEmployeeComponent.


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

2 <app-sidebar></app-sidebar>

src/app/employee/list-employee/list-employee.component.ts:5:16 5 templateUrl: './list-employee.component.html', ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 组件ListEmployeeComponent的模板出错。

错误:src/app/employee/list-employee/list-employee.component.html:514:9 - 错误 NG8001:'app-footer' 不是已知元素:

  1. 如果“app-footer”是一个 Angular 组件,则验证它是该模块的一部分。
  2. 如果“app-footer”是 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“@NgModule.schemas”以禁止显示此消息。

514 ~~~~~~~~~~~~

src/app/employee/list-employee/list-employee.component.ts:5:16 5 templateUrl: './list-employee.component.html', ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 组件ListEmployeeComponent的模板出错。

请帮我解决这个错误。

【问题讨论】:

  • 您应该将页眉、页脚和侧边栏组件放在一个单独的模块中,您可以将其导入所有具有需要显示它们的组件的模块中。
  • 我分别创建了页眉、页脚、侧边栏组件。
  • 在另一个模块中?然后您需要做的就是在EmployeeModule 中导入声明(并导出)这些组件的模块,它应该可以工作。

标签: javascript angular


【解决方案1】:

如果您将这些组件放在单独的模块中,您可以将其导入应用程序的所有其他使用侧边栏、页眉和页脚的部分。

@NgModule({
  declarations: [
    SidebarComponent,
    HeaderComponent,
    FooterComponent
  ],
  exports: [
    SidebarComponent,
    HeaderComponent,
    FooterComponent
  ]
})
export class LayoutModule { }

在员工模块中,您可以执行以下操作:

@NgModule({
  declarations: [
    // ...
  ],
  imports: [
    // ...
    LayoutModule,
  ],
})
export class EmployeeModule {}

【讨论】:

  • 谢谢你。会检查并回复
  • 嗨兄弟,它的工作,现在显示另一个错误,如 Error: src/app/footer/footer.component.ts:8:14 - error NG6004: Present in the NgModule.exports of EmployeeModule 但两者都没有声明或导入
  • 看起来您错过了还需要在 declarations 数组内指定组件名称的步骤,而不仅仅是在 exports 内。这次的错误描述性很强。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多