我正在尝试访问通过ngComponentOutlet创建的子组件,但ViewChildren始终返回一个空列表。对此有不同的方法吗?
我尝试了什么:
app.component.ts
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
entryComponents: [
HomeComponent
]
})
export class AppComponent implements AfterViewInit {
pageSections = [
HomeComponent,
HomeComponent
];
@ViewChildren(HomeComponent)
pageSectionElems: QueryList<HomeComponent>;
ngAfterViewInit() {
console.log(this.pageSectionElems);
}
}
app.component.html
<div *ngFor="let pageSection of pageSections">
<ng-container *ngComponentOutlet="pageSection"></ng-container>
</div>