Angular ContentChildren(Component) 获取任何类型

Angular ContentChildren(Component) get any type

我知道您可以通过指定特定的组件类型来获取子组件 @ContentChildren(SpecificComponent) 但是我可以使用 @ContentChildren(SOMETHING) 并获取一个组件而不考虑其类型,例如通用组件吗?

不确定为什么有人在不说明原因的情况下否决了这个,但最终找到了解决方案:

@ContentChildren('childComponent') childComponents;

我将字符串而不是组件传递给内容子项。该字符串允许我使用参考选择器 (#childComponent)

@Component({
  template: '<ng-content></ng-content>',
})
export class TestComponent implements AfterContentInit {

    constructor(private readonly elementRef: ElementRef) {
    }

    ngAfterContentInit() {
        const element = this.elementRef.nativeElement as HTMLElement;
        // do wathever you want with element.children
    }
}