【问题标题】:ng test '<component> is not a known element'ng test '<component> 不是已知元素'
【发布时间】:2017-06-13 12:05:15
【问题描述】:

我正在使用 angular 2,当我使用 ng test --build=false --watch=false 运行测试时收到此消息:

'app-feed' is not a known element:
1. If 'app-feed' is an Angular component, then verify that it is part of this mod
2. If 'app-feed' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@Ng
[ERROR ->]<app-feed></app-feed>**

app.component.html 文件内容:

<app-menu></app-menu>
<app-feed></app-feed>

app.component.ts 文件内容:

import { Component } from '@angular/core';

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

app.module.ts 文件内容:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { MenuComponent } from './menu/menu.component';
import { FeedComponent } from './feed/feed.component';

@NgModule({
  declarations: [AppComponent,MenuComponent,FeedComponent],
  imports: [BrowserModule, FormsModule,HttpModule],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

feed.component.ts 文件内容:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-feed',
  templateUrl: './feed.component.html',
  styleUrls: ['./feed.component.css']
})
export class FeedComponent implements OnInit {

  constructor() { }

  ngOnInit() {}

}

谁能帮我弄清楚如何解决这个问题?

【问题讨论】:

标签: components element


【解决方案1】:

如其他地方所述,您应该发布您的规范文件。

我找到了一种通过使用模块来解决与此非常相似的问题的方法。在 feed/feed.module.ts 创建一个新模块并在其中导入您的 FeedComponent:

import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {FeedComponent} from './feed.component';

@ngModule({
    imports: [CommonModule],
    declarations: [FeedComponent],
    exports: [FeedComponent]
})
export class FeedModule {}

将您的 app.module.ts 更改为:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { FeedModule } from './feed/feed.module';

import { AppComponent } from './app.component';
import { MenuComponent } from './menu/menu.component';

@NgModule({
  declarations: [AppComponent,MenuComponent],
  imports: [BrowserModule, FormsModule, HttpModule, FeedModule],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

然后,在规范文件中包含 feed 模块:

import {FeedModule} from 'path/to/feed.module';
...
TestBed.configureTestingModule({
    ...
    imports: [FeedModule, ...]
    ...
})

【讨论】:

    猜你喜欢
    • 2017-11-14
    • 2021-09-02
    • 2017-01-13
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 2017-07-27
    相关资源
    最近更新 更多