Angular 2 RC6:“模式列表"不是已知元素 [英] Angular 2 RC6: 'pattern-list' is not a known element

查看:51
本文介绍了Angular 2 RC6:“模式列表"不是已知元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单地运行应用程序,我没有任何错误,并且工作正常,但是当我运行测试时,出现以下错误:

Simply running the application I get no error and it works just fine, but when I run my tests I get the following error:

'pattern-list' is not a known element:
  1. If 'pattern-list' is an Angular component, then verify that it is part of this module.
  2. If 'pattern-list' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schema' of this component to suppress this message. ("
         [ERROR ->]<pattern-list></pattern-list>

当我仅使用'npm-start'运行应用程序时,我首先遇到了此问题,并解决了它,在声明部分将必需的组件添加到app.module中.但是现在,当我要测试时,我遇到了相同的错误,我不知道为什么. 这是我的代码:

I had this issue first when I just run the application with 'npm-start' and I solved it adding the required component to app.module in the declarations section. But now as I want to test I get the same error and I don't know why. Here is my code:

app.module.ts

app.module.ts

@NgModule({
  imports: [ BrowserModule, FormsModule, HttpModule, ReactiveFormsModule ],
  declarations: [ AppComponent, PatternListComponent, PatternDetailComponent, WidgetListComponent,
    FormComponent, DefaultWidget, LabelComponent, CheckboxWidget ],
  bootstrap: [ AppComponent ],
  providers: [ WidgetService ]
})
export class AppModule { }

app.component.ts

app.component.ts

@Component({
     selector: 'my-app',
     template: `
           <pattern-list></pattern-list>
          `
    })
    export class AppComponent { }

pattern.list.component:

pattern.list.component:

@Component({
  selector: 'pattern-list',
  template: `
    <div class="patterns">
      <pattern-detail *ngFor="let p of patternDetails" [metadata]="p" 
        (selectPattern)="selectPattern(p)"></pattern-detail>
    </div>
    <div *ngIf="selectedPattern" class="widget-list">
      <widget-list [pattern]="selectedPattern">
      </widget-list>
    </div>
  `,
  styleUrls: ['/css/styles.css']
})
export class PatternListComponent implements OnInit{
  selectedPattern: PatternDetails;
  constructor(private http: Http) {
  }

  patternDetails: PatternDetails[];

  ngOnInit() {
    this.getPatterns();
  }

  getPatterns() {
    this.http.get('/app/assets/patternDetails.json')
      .map((res:Response) => res.json() )
      .subscribe(
        data => { this.patternDetails = data.patternList; },
        err => console.error('The problem is: ' + err),
        () => console.log('done')
      );
    console.log(this.patternDetails);
  }

  selectPattern(pattern: PatternDetails) {
    this.selectedPattern = pattern;
    this.setSelectedProperty(pattern);
  }

  setSelectedProperty(selectedPattern: PatternDetails) {
    for (var p in this.patternDetails) {
      if (this.patternDetails[p] == selectedPattern) {
        this.patternDetails[p].selected = true;
      } else {
        this.patternDetails[p].selected = false;
      }
    }
  }
}

我的测试文件:app.component.spec.ts

My test file: app.component.spec.ts

describe('AppComponent with TCB', function () {
  beforeEach(() => {
    TestBed.configureTestingModule({declarations: [AppComponent]});
  });
  describe('asdfasdf', function () {
    beforeEach(async(() => {
      TestBed.compileComponents();
    }));
    it('should instantiate component', () => {
      let fixture = TestBed.createComponent(AppComponent);
      expect(fixture.componentInstance instanceof AppComponent).toBe(true, 'should create AppComponent');
    });
  });
});

我正在使用webpack,不确定是否很重要.

I'm using webpack, I'm not sure if that matters.

推荐答案

我认为您需要

TestBed.configureTestingModule({imports: [AppModule]});

这篇关于Angular 2 RC6:“模式列表"不是已知元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆