angular - 使用带有 resolveComponentFactory 的通用组件会导致 Component<{}> 而不是 <T>

标签 angular angular-components angular-dynamic-components

我已经构建了一个对话框服务,它动态地创建了一个带有子组件的 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/

相关文章:

javascript - Angular2/JS - 日期表达式仅适用于 Chrome

如果通过具有绑定(bind)的动态组件插入多个 DOM 元素,Angular 会滞后性能

dart - Dart Angular-在手机上获取滑动事件

angular - 如何保持悬停时显示动态弹出窗口

angular - 在组件函数中选择 angular 模板,而不是在 @Component 中添加

javascript - 将 jQuery 插件导入 Angular 2+ 拒绝执行脚本,因为它的 MIME 类型 ('text/html' ) 不可执行

javascript - 将启用/禁用按钮的复选框

class - 如何使用 TypeScript 在 Angular 2 组件中声明模型类?

css - 如何从父组件的 CSS 文件中设置子组件的样式?

javascript - AngularJS:使用 ngRoute 路由时未加载组件 Controller