용액으로부터 취한 구현의 일례이다. 매개 변수가 추상 클래스라는 중요한 사실 중 하나입니다. 인터페이스를 사용할 수 없습니다. 실제로 추상 클래스도 사용할 수 없지만 중첩 된 구성 요소에 공급자를 사용합니다. 내 경우 는 추상 클래스를 확장하고 공급자로이 추상 클래스를 선언해야
export class FilterComponent{
@ContentChildren(FilterItem) filterItems: QueryList<FilterItem>;
constructor() {}
getItems(){
console.log(this.filterItems);
this.filterItems.forEach(i => {
console.log('My name is ' + i.name + 'My value is ' + i.getValue());
});
}
}
2) 중첩 된 구성 요소입니다. 내 경우
@Component({
selector: 'app-string-filter-item',
templateUrl: './string-filter-item.component.html',
styleUrls: ['./string-filter-item.component.scss'],
providers: [{provide: FilterItem, useExisting: forwardRef(() => StringFilterItemComponent)}]
})
export class StringFilterItemComponent extends FilterItem {
selectValue: string;
@Input()
name:string;
caption: 'SHow me smt';
getValue(){
return this.selectValue;
}
}
문자열 필터 item.component.html 필터 구성 요소를 사용하여
<p>
<input type="text" [(ngModel)]="selectValue">
</p>
filter.component.html
<div class="filter-wr">
<ng-content></ng-content>
</div>
어디서나 당신이 합니다 (선택 문자열 구성 요소에서 내가 사용하는 또 다른 구성 요소입니다)
<app-filter>
<app-select-filter-item name="first"></app-select-filter-item>
<app-string-filter-item name="second"></app-string-filter-item>
<app-select-filter-item name="third"></app-select-filter-item>
<app-string-filter-item name="fourth"></app-string-filter-item>
</app-filter>
그게 전부입니다! 관심을 가져 주셔서 감사합니다!
무엇을하고 싶습니까? 동일한 구성 요소에서 구성 요소의 인스턴스를 가져 오시겠습니까? 우리는 대답 할 수 있도록 당신의 질문을 더 많이 설명 할 수 있습니다. –