在AppComponent中,我在HTML代码中使用nav组件。UI看起来很好。做服务时没有错误。当我看这个应用程序时,控制台上没有任何错误。
但是当我为我的项目运行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';发布于 2017-06-12 20:54:02
因为在单元测试中,您希望测试的组件主要是与应用程序的其他部分隔离的,因此在默认情况下,角不会添加组件、服务等模块的依赖项。所以你需要在你的测试中手动完成。基本上,这里有两种选择:
( A)在测试中声明原始NavComponent
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent,
NavComponent
]
}).compileComponents();
}));( B)伪装NavComponent
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent,
MockNavComponent
]
}).compileComponents();
}));
// it(...) test cases
});
@Component({
selector: 'app-nav',
template: ''
})
class MockNavComponent {
}您将在正式文件中找到更多信息。
发布于 2018-03-27 19:17:44
您也可以使用NO_ERRORS_SCHEMA
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent
],
schemas: [NO_ERRORS_SCHEMA]
}).compileComponents();
}));发布于 2019-11-04 10:08:54
对我来说,导入父程序中的组件解决了这个问题。
describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AppComponent,
NavComponent
]
}).compileComponents();
}));在使用此组件的spec of the parent中添加此组件。
https://stackoverflow.com/questions/44504468
复制相似问题