投影视图容器中的Angular 6查询组件

时间:2018-08-03 09:48:52

标签: angular angular6

以下是该问题的一个示例:https://stackblitz.com/edit/angular-vvpr21 当使用ComponentFactoryResolver动态创建组件时,父级的ContentChildren装饰器将忽略它们。我正在尝试从另一个角度解决这个问题。

我想查询一个InjectionToken并通过contentchildren获得组件的引用。

export const ChildToken = new InjectionToken<ChildComponent>('ChildToken');
@Component({
  ...
  providers: [{
    provide: ChildToken,
    useExisting: forwardRef(() => ChildComponent)
  }]
  ...
}) export class ChildComponent{ ... }

因此我可以在我的ParentComponent所在的位置导入ChildToken并尝试以下操作:

@Component({..}) export class ParentComponent{
  ...
  @ContentChildren(ChildToken) children:QueryList<ChildComponent>;
  ...
}

在这里,我从ts中收到此错误:

“ InjectionToken”类型的参数不能分配给'string | Function | Type'类型的参数。   类型“ InjectionToken”不可分配给类型“ Type”。     类型'InjectionToken'中缺少属性'apply'。”

之后,我也尝试了此操作:

@ContentChildren(ChildToken.ngInjectableDef) children:QueryList<ChildComponent>;

在这里,我从js中收到此错误:

“未捕获的错误:由于未定义查询选择器,因此无法为“ ParentComponent”的属性“ children”构造查询。”

0 个答案:

没有答案
相关问题