当前位置: 首页 > 面试题库 >

Angular 2 Karma Test'component-name'不是一个已知元素

古起运
2023-03-14
问题内容

在AppComponent中,我在HTML代码中使用了nav组件。用户界面看起来不错。服务时没有错误。当我查看应用程序时,控制台中没有错误。

但是,当我为我的项目运行Karma时,出现了一个错误:

Failed: Template parse errors: 
'app-nav' is not a known element:
1. If 'app-nav' is an Angular component, then verify that it is part of this module.
2. If 'app-nav' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

在我的 app.module.ts中

有:

import { NavComponent } from './nav/nav.component';

它也在NgModule的声明部分中

@NgModule({
  declarations: [
    AppComponent,
    CafeComponent,
    ModalComponent,
    NavComponent,
    NewsFeedComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    JsonpModule,
    ModalModule.forRoot(),
    ModalModule,
    NgbModule.forRoot(),
    BootstrapModalModule,
    AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})

NavComponent在我的AppComponent

app.component.ts

import { Component, ViewContainerRef } from '@angular/core';
import { Overlay } from 'angular2-modal';
import { Modal } from 'angular2-modal/plugins/bootstrap';
import { NavComponent } from './nav/nav.component';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = "Angela";
}

app.component.html

<app-nav></app-nav>
<div class="container-fluid">
</div>

我已经看到了类似的问题,但是该问题的答案表明我们应该在具有导出功能的nav组件中添加NgModule,但是这样做时会出现编译错误。

还有: app.component.spec.ts

import {NavComponent} from './nav/nav.component';
import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';

问题答案:

因为在单元测试中,您想测试大部分与应用程序其他部分隔离的组件,所以默认情况下,Angular不会添加模块的依赖项,例如组件,服务等。因此,您需要在测试中手动执行此操作。基本上,这里有两个选择:

A)在测试中声明原始NavComponent

describe('AppComponent', () => {
  beforeEach(async(() => {
      TestBed.configureTestingModule({
        declarations: [
          AppComponent,
          NavComponent
        ]
      }).compileComponents();
    }));

B)模拟导航组件

describe('AppComponent', () => {
  beforeEach(async(() => {
      TestBed.configureTestingModule({
        declarations: [
          AppComponent,
          MockNavComponent
        ]
      }).compileComponents();
    }));

// it(...) test cases

});

@Component({
  selector: 'app-nav',
  template: ''
})
class MockNavComponent {
}

您可以在官方文档中找到更多信息。



 类似资料:
  • 我正在尝试在其他模块中使用我在AppModule内部创建的组件。但我得到了以下错误: msgstr"未捕获(promise中):错误:模板解析错误: “联系人框”不是已知元素: 如果“触点盒”是一个角度组件,则确认它是该模块的一部分 如果“联系人框”是Web组件,则将“自定义元素”模式添加到“@NgModule”。“此组件的架构”以抑制此消息 我将我的页面保存在页面目录中,其中每个页面保存在不同的

  • 在Kafka Streams中添加压缩配置,类似于此链接: 但是我在日志中看到以下警告消息: 这个设置不受尊重吗?是不是表达得不好?

  • 我正在使用角CLI和角材料v.5.2.5,并试图使用 mat图标按钮 但是控制台会产生这样的错误: 未捕获的错误:模板解析错误:'mat-icon'不是一个已知的元素... 如果我使用 垫高按钮 一切正常,不知道如何解决这个问题 指数html main.ts 应用程序。单元ts

  • 在AppComponent中,我使用超文本标记语言代码中的导航组件。用户界面看起来不错。做ng服务时没有错误。当我查看应用程序时,控制台中没有错误。 但是当我为我的项目运行Karma时,有一个错误: 在我的app.module.ts: 有: 它也在NgModule的声明部分 我在我的中使用 应用程序组件.ts app.component.html 我看到过一个类似的问题,但该问题的答案是,我们应该

  • 我用的是angular2 beta 15,在我的一个模板中我做到了: 但是,我得到了这个例外 因此,如何解决?有什么想法?

  • 我正在尝试将AGM Angular谷歌地图(https://github.com/SebastianM/angular-google-maps)添加到我的Angular4项目中 指南是:https://github.com/SebastianM/angu 当我访问带有agm组件的页面时遇到的错误: 我已经确定/尝试过; 我的模块导入 在我的模块中向架构添加了CUSTOM_ELEMENTS_SCHE