[英]Angular 2 dynamic component generation, ts compilation error
I can create dynamic component by using this method: 我可以使用以下方法创建动态组件:
public addItem<T extends WidgetComponent>(ngItem: {new(): T}): T {
let factory = this._componentFactoryResolver.resolveComponentFactory(ngItem);
const ref = this._viewCntRef.createComponent(factory);
const newItem: T = ref.instance as T;
...
return newItem;
}
And call it like this: 并这样称呼它:
const ref: MyWidgetComponent = this.dashboard.addItem<MyWidgetComponent>(MyWidgetComponent);
But typescript put me this compilation error: app.component.ts:45:35 Untyped function calls may not accept type arguments.
但是打字稿给我带来了这个编译错误:
app.component.ts:45:35 Untyped function calls may not accept type arguments.
I try to replace {new(): T}
by Type<T>
but I have the same error: app.component.ts:45:35 Untyped function calls may not accept type arguments.
我尝试用
Type<T>
替换{new(): T}
,但出现相同的错误: app.component.ts:45:35 Untyped function calls may not accept type arguments.
What is the correct definition here ? 这里的正确定义是什么? Because the code work great...
因为代码效果很好...
EDIT: here is the full code if you want to see it in place https://github.com/jaumard/ng2-dashboard/blob/master/components/dashboard/dashboard.component.ts#L99 编辑:这是完整的代码,如果您想在原处查看它的话https://github.com/jaumard/ng2-dashboard/blob/master/components/dashboard/dashboard.dash.component#ts#L99
I manage to fix compilation error by changing the signature to : 我设法通过将签名更改为来修复编译错误:
public addItem(ngItem: Type<WidgetComponent>): WidgetComponent
And the call like this : 和这样的电话:
const ref: MyWidgetComponent = this.dashboard.addItem(MyWidgetComponent) as MyWidgetComponent;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.