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

在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';


哈士奇WWW
浏览 918回答 3
3回答
打开App,查看更多内容
随时随地看视频慕课网APP