溶液から採取した実装の一例です。パラメータが抽象クラスであることが重要です。私たちはそこでインターフェイスを使用することはできません。実際には抽象クラスも使用できませんが、ネストされたコンポーネントにプロバイダを使用しています。私の場合 それは抽象クラスを拡張し、プロバイダーとして、この抽象クラスを宣言する必要があり
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>
これですべてです!ご注意いただきありがとうございます!
あなたは何をしたいのですか?あなたは同じコンポーネント内のコンポーネントのインスタンスを取得したいですか?単にインスタンスを取得するために 'this'を使用することができます。そうでなければ、私たちは答えることができるようにあなたの質問をもっと説明することができます。 –