Angular ContentChildren: Accessing Sub-Components of the Same Type Using forwardRef
If you want to use `ContentChildren` to get references to sub-components of the same type in Angular, you should use the `forwardRef` function to create a reference to the component. Here's how you can modify your code: 1. Import the necessary modules: ``typescript`` import { Component, ContentChildren, QueryList, forwardRef } from '@angular/core'; `` 2. Create a reference to the component using `forwardRef`: ``typescript`` @Component({ selector: 'my-component', template: '...', }) export class MyComponent { @ContentChildren(forwardRef(() => MyComponent)) childrenComponents: QueryList<MyComponent>; } `` By using `forwardRef`, you are allowing `MyComponent` to be referenced before its declaration is complete. Make sure you have imported the `forwardRef` function from `@angular/core` and that you have correctly declared and exported the `MyComponent` class.
原文地址: https://www.cveoy.top/t/topic/qc9k 著作权归作者所有。请勿转载和采集!