我已经构建了一个对话框服务,它动态地创建了一个带有子组件的 DialogComponent。
我希望我的 DialogComponent 是一个泛型类 <T>
因为我希望为我正在使用的任何组件 child 输入。我目前正在使用这些行创建我的 DialogComponent ->
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(DialogComponent);
const componentRef = componentFactory.create(new DialogInjector(this.injector, map));
问题是 resolveComponentFactory 实际上返回了
DialogComponent<{}>
而不是 T
.我确实尝试过转换,但似乎我不能,因为缺少某些方法。我想知道我怎么能做到这一点!
谢谢。
编辑
this.componentFactoryResolver.resolveComponentFactory<DialogComponent<T>>
做的伎俩..
最佳答案
要获得正确的类型,您需要在 resolveComponentFactory
的泛型类型中传递想要的类型。 :
this.componentFactoryResolver.resolveComponentFactory<DialogComponent<T>>(DialogComponent);
关于angular - 使用带有 resolveComponentFactory 的通用组件会导致 Component<{}> 而不是 <T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54693186/