使用NgContentSelectorHelper类来解决这个问题。这个类会自动将选择器匹配到正确的内容投影上。
以下是具体的代码实现:
首先,在组件类中添加import语句:
import { NgContentSelectorHelper } from './ng-content-selector-helper';
然后在组件类中定义一个helper变量:
private helper: NgContentSelectorHelper;
在组件的构造函数中实例化helper变量:
constructor(private elementRef: ElementRef) { this.helper = new NgContentSelectorHelper(this.elementRef); }
最后,使用helper变量的project方法来进行内容投影:
ngAfterContentInit() { this.helper.project('slot1'); }
这里的'slot1'是一个投影插槽的名称。确保使用正确的名称。
这样就可以解决多插槽内容投影在Angular组件中无法工作的问题了。