角测试错误,即使组件中存在属性

时间:2018-06-27 17:11:58

标签: javascript angular

运行ng test时出现以下错误:

Can't bind to 'foobar' since it isn't a known property of 'app-my-item'.

1. If 'app-my-item' is an Angular component and it has 'foobar' input, then verify that it is part of this module.
2. If 'app-my-item' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.

组件:

import { Component, Input, OnInit, Renderer, EventEmitter, Output, OnChanges, SimpleChanges } from '@angular/core';

@Component({
  selector: 'app-my-item',
  templateUrl: './app-item.component.html',
  styleUrls: ['./app-item.component.scss'],
})

export class MyItemComponent implements OnInit, OnChanges {

  @Input() video;
  @Input() foobar = <string> '';
  buttonClass = 'green';

  // Code....

  ngOnChanges({ foobar: { currentValue } }: SimpleChanges): void {
    if (currentValue) {
      this.buttonClass = (
        currentValue === this.video.key ? 'blue' : 'green'
      );
    }
  }

  // Code....

}

查看:

  <app-my-item
      //Code ....
      [foobar]="foobar"
      //Code ....
  >
  </app-my-item>

我不明白为什么会收到错误消息,因为foobar实际上是在组件中定义为输入的。如何解决此错误?

0 个答案:

没有答案