Javascript 即使组件中存在属性,角度测试错误
运行Javascript 即使组件中存在属性,角度测试错误,javascript,angular,Javascript,Angular,运行ng测试时,我遇到以下错误: 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
ng测试时,我遇到以下错误:
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>
import{Component,Input,OnInit,Renderer,EventEmitter,Output,OnChanges,SimpleChanges}来自'@angular/core';
@组成部分({
选择器:“应用程序我的项目”,
templateUrl:'./app item.component.html',
样式URL:['./app item.component.scss'],
})
导出类MyItemComponent实现OnInit、OnChanges{
@输入()视频;
@输入()foobar='';
buttonClass='绿色';
//代码。。。。
ngOnChanges({foobar:{currentValue}}}:SimpleChanges):void{
if(当前值){
this.buttonClass=(
currentValue==this.video.key?'blue':'green'
);
}
}
//代码。。。。
}
查看:
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
实际上是在组件中定义为输入的。如何修复此错误?是否有可能对空字符串的默认赋值使其失去跟踪?@MotKohn刚刚更改@Input()foobar=''
到@Input()foobar代码>和同样的错误:(你能添加你的模块文件吗?你在测试哪个组件?应用程序项
或其他组件?