失败:模板解析错误:“ app-header”不是已知元素

时间:2019-12-15 14:20:34

标签: typescript karma-jasmine karma-runner angular8

homepage.module.ts

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from "@angular/core";
import { CommonModule } from "@angular/common";
import { HomepageComponent } from "./homepage.component";
import { SharedModule } from "../shared/shared.module";


@NgModule({
  declarations: [HomepageComponent],
  imports: [
    CommonModule,
    SharedModule
  ],
  exports: [HomepageComponent],
  providers: [],
  schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class HomepageModule {}


shared-module.ts

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from "@angular/core";
import { CommonModule } from "@angular/common";
import { HeaderComponent } from "./header/header.component";
@NgModule({
  declarations: [
    HeaderComponent,
  ],
  imports: [
    CommonModule,
  ],
  exports: [
    HeaderComponent,
  ],
  schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class SharedModule {}


homepage.component.html

<app-header (toggleSideBarForMe)="sideBarToggler($event)"></app-header>


app.module.ts

import { HomepageModule } from "./homepage/homepage.module";
@NgModule({
  declarations: [AppComponent],
  imports: [
    HomepageModule,
    ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

运行业力时,我遇到了错误。 : 失败:模板解析错误: “ app-header”不是已知元素: 1.如果“ app-header”是Angular组件,请验证它是否属于此模块。 2.如果“ app-header”是Web组件,则将“ CUSTOM_ELEMENTS_SCHEMA”添加到该组件的“ @ NgModule.schemas”以禁止显示此消息。 (“ [错误->]

我是否需要在homepage.component.spec文件中添加任何内容?

1 个答案:

答案 0 :(得分:0)

  • 尝试查看必须使用“ app-header”和HeaderComponent的组件是否也已导入模块中
  • 尝试查看您的组件之一是否未在2个或多个模块中导入