通用类型'ComponentRef <c>'需要1个类型的参数

时间:2016-07-01 03:36:54

标签: angular ionic2 angular2-components

无法移除ionic-2中的动态组件。在打字稿编译时说它是异常

  

“通用类型'ComponentRef'需要1个类型的参数”。

此外,在不使用ionic2的情况下使用相同的代码。 非常感谢你的帮助。 在此先感谢。

class DynamicCmp {
  _ref: ComponentRef;
  _idx: number;
  constructor(private resolver: ComponentResolver, private location: ViewContainerRef) { }
  remove() {
    this._ref.destroy();
  }
  add1() {
    this.resolver.resolveComponent(DynamicCmp).then((factory: ComponentFactory<any>) => {
      let ref = this.location.createComponent(factory, 0);
      ref.instance._ref = ref;
      ref.instance._idx = this._idx++;
    });
  }
}
  

异常:TypeScript错误:...... / home / home.ts(9,11):Erro r   TS2314:泛型类型'ComponentRef'需要1个类型的参数。

1 个答案:

答案 0 :(得分:24)

ComponentRef是一种通用类型。只需更改以下代码:

class DynamicCmp {
  _ref: ComponentRef<any>; <== add <any>

希望它可以帮到你!

相关问题