我无法从父级访问子组件。以下代码为数组长度输出0我预计3个孩子?此外,myChildren observable从未收到更改事件;因此,我不认为这是一个时间问题。
import {Component, ContentChildren, QueryList, ngAfterContentInit} from '@angular/core';
import {ChildComponent} from './child'
@Component({
selector: 'parent-component',
template: `
<div class='box-container'>
<p>I'm the parent</p>
<child-component></child-component>
<child-component></child-component>
<child-component></child-component>
</div>
`,
directives: [ChildComponent]
})
export class ParentComponent implements AfterContentInit {
@ContentChildren(ChildComponent, {descendants: true}) myChildren: QueryList<ChildComponent>;
ngAfterContentInit( ) {
console.log(this.myChildren.toArray().length)
this.myChildren.changes.subscribe(_ => {
console.log(myChildren.length);
});
}
}