【发布时间】:2018-06-24 11:22:46
【问题描述】:
我的应用程序中有一个名为 pq-button 的组件:
<div class="container mt-5" *ngIf="categoryData">
<div class="row">
<div class="col">
<h5 class="text-center">I am looking for a
<pq-button [list]="listCategories" (onSelect)="setCategory($event)" title="{{ category ? category.name : 'Category' }}"></pq-button> that can
<pq-button [list]="listCriteria" [disabled]="!category" (onSelect)="setCategory($event)" title="{{ criteria ? criteria.name : 'Criteria' }}"></pq-button> and is good for
<pq-button [list]="categoryData.list" [disabled]="!category" (onSelect)="setCategory($event)" title="{{ persona ? persona.name : 'Persona' }}"></pq-button></h5>
</div>
</div>
</div>
当我加载我的应用程序时,它可以正常工作。
然后当我运行ng test 时,我得到的第一个错误是:
AppComponent 应该创建应用程序 失败:模板解析错误: 无法绑定到“list”,因为它不是“pq-button”的已知属性。
它说的第一件事是确保它是这个模块的一部分。 所以,当我查看我的模块时,我会看到:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';
import { SharedModule } from './shared/shared.module';
import { SelectorComponent } from './selector/selector.component';
import { ButtonComponent } from './button/button.component';
@NgModule({
declarations: [
AppComponent,
SelectorComponent,
ButtonComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
HttpClientModule,
SharedModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
所以它是它的一部分。有谁知道它会失败的任何其他原因吗?
根据要求,这是 pq-button 组件和 pq-selector 组件:
观点:
<button class="btn" type="button btn-default" [ngClass]="{ 'btn-active': selected }" [disabled]="disabled" (click)="openSelector = !openSelector">{{ title }}</button>
<pq-selector [items]="items" colour="#e95344" (onSelect)="set($event)" [(visible)]="openSelector"></pq-selector>
代码:
import { Component, Input, Output, EventEmitter, OnChanges } from '@angular/core';
@Component({
selector: 'pq-button',
templateUrl: './button.component.html',
styleUrls: ['./button.component.scss']
})
export class ButtonComponent implements OnChanges {
@Input() list: Function;
@Input() title: string;
@Input() disabled: boolean;
@Output() onSelect = new EventEmitter<string>();
items: any[];
constructor() { }
ngOnChanges() {
if (!this.disabled)
this.list().subscribe(response => this.items = response);
}
set($event) {
this.onSelect.emit($event);
}
}
还有选择器html:
<div class="container-fluid selector" [ngStyle]="{'background-color': colour}" [@dialog] *ngIf="visible">
<div class="row h-100 justify-content-center">
<div class="col-2 my-auto">
<ul class="list-unstyled">
<li *ngFor="let item of items"><a href="#" (click)="select(item)">{{ item.name }}</a></li>
</ul>
</div>
</div>
</div>
<button type="button" class="close" aria-label="Close" (click)="close()">
<span aria-hidden="true">×</span>
</button>
和代码:
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import {
trigger,
state,
style,
animate,
transition
} from '@angular/animations';
@Component({
selector: 'pq-selector',
templateUrl: './selector.component.html',
styleUrls: ['./selector.component.scss'],
animations: [
trigger('dialog', [
transition('void => *', [
style({ transform: 'scale3d(.3, .3, .3)' }),
animate(100)
]),
transition('* => void', [
animate(100, style({ transform: 'scale3d(.0, .0, .0)' }))
])
])
]
})
export class SelectorComponent implements OnInit {
@Input() items: any[];
@Input() colour: string;
@Input() closable = true;
@Input() visible: boolean;
@Output() onSelect = new EventEmitter<string>();
@Output() visibleChange: EventEmitter<boolean> = new EventEmitter<boolean>();
private show: boolean;
constructor() { }
ngOnInit() {
this.colour = this.colour || '#000000';
}
select(item) {
this.onSelect.emit(item);
this.close();
}
close() {
this.visible = false;
this.visibleChange.emit(this.visible);
}
}
希望对你有帮助!
【问题讨论】:
-
贴出pq-button组件.ts的代码
-
@r3plica - 你找到解决方案了吗?这似乎是一个常见问题,通常根源于 spec.ts 没有所需的导入,但我的情况与您的情况相似,仍在寻找令人满意的纠正措施。
-
我可能有一个解决方案,但 r3plica 需要对其进行测试才能成为答案。 pq-button 需要导入到测试包含 pq-button 的组件的 spec.ts 中。然后应将其添加到 TestBed 配置的声明中。
标签: angular karma-jasmine