泛型类型'ComponentRef'需要一个类型参数

时间:2022-11-27 23:01:57

Unable to remove dynamic components in ionic-2. It’s saying exception while typescript compile

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

“Generic type 'ComponentRef' requires 1 type argument(s)”.

“通用类型”组件需要1个类型参数。

Also, the same code is working while using without using ionic2. Much appreciate your help. Thanks in Advance.

同样,同样的代码在不使用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++;
    });
  }
}

Exception: TypeScript error: ....../home/home.ts(9,11): Erro r TS2314: Generic type 'ComponentRef' requires 1 type argument(s).

例外:打字稿错误:……/home/home.ts(9,11): Erro r TS2314:泛型类型‘ComponentRef’需要一个类型参数。

1 个解决方案

#1


20  

ComponentRef is a generic type. Just change your code the following:

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

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

Hope it helps you!

希望它能帮助你!

#1


20  

ComponentRef is a generic type. Just change your code the following:

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

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

Hope it helps you!

希望它能帮助你!