Angular 2 动态组件生成,ts编译错误
Angular 2 dynamic component generation, ts compilation error
我可以用这个方法创建动态组件:
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;
}
并这样称呼它:
const ref: MyWidgetComponent = this.dashboard.addItem<MyWidgetComponent>(MyWidgetComponent);
但是打字稿给我这个编译错误:
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.
这里正确的定义是什么?因为代码很好用...
编辑:这里是完整的代码,如果你想看到它https://github.com/jaumard/ng2-dashboard/blob/master/components/dashboard/dashboard.component.ts#L99
我设法通过将签名更改为来修复编译错误:
public addItem(ngItem: Type<WidgetComponent>): WidgetComponent
这样的调用:
const ref: MyWidgetComponent = this.dashboard.addItem(MyWidgetComponent) as MyWidgetComponent;
我可以用这个方法创建动态组件:
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;
}
并这样称呼它:
const ref: MyWidgetComponent = this.dashboard.addItem<MyWidgetComponent>(MyWidgetComponent);
但是打字稿给我这个编译错误:
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.
这里正确的定义是什么?因为代码很好用...
编辑:这里是完整的代码,如果你想看到它https://github.com/jaumard/ng2-dashboard/blob/master/components/dashboard/dashboard.component.ts#L99
我设法通过将签名更改为来修复编译错误:
public addItem(ngItem: Type<WidgetComponent>): WidgetComponent
这样的调用:
const ref: MyWidgetComponent = this.dashboard.addItem(MyWidgetComponent) as MyWidgetComponent;