在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在我的AppComponentapp.component.tsimport { 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.tsimport {NavComponent} from './nav/nav.component';import { TestBed, async } from '@angular/core/testing';import { AppComponent } from './app.component';
Angular 2 Karma Test'component-name'不是一個已知元素
哈士奇WWW
2019-12-27 10:38:37