【问题标题】:Angular 2 dynamic component generation, ts compilation errorAngular 2动态组件生成,ts编译报错
【发布时间】:2016-11-16 13:59:34
【问题描述】:

我可以使用这个方法创建动态组件:

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);

但是 typescript 给我这个编译错误: app.component.ts:45:35 Untyped function calls may not accept type arguments.

我尝试用Type&lt;T&gt; 替换{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

【问题讨论】:

    标签: angularjs angular typescript compilation angular2-components


    【解决方案1】:

    我设法通过将签名更改为来修复编译错误:

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

    还有这样的电话:

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

    【讨论】:

      猜你喜欢
      • 2017-10-21
      • 2023-03-24
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 2017-02-26
      • 2021-07-25
      • 2017-05-12
      • 1970-01-01
      相关资源
      最近更新 更多