簡體   English   中英

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.dash.component#ts#L99

我設法通過將簽名更改為來修復編譯錯誤:

public addItem(ngItem: Type<WidgetComponent>): WidgetComponent

和這樣的電話:

const ref: MyWidgetComponent = this.dashboard.addItem(MyWidgetComponent) as MyWidgetComponent;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM