Generic type 'ComponentRef<C>' requires 1 type argument(s)

user2932411 picture user2932411 · Jul 1, 2016 · Viewed 11.4k times · Source

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

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

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

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

Answer

yurzui picture yurzui · Jul 1, 2016

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

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

Hope it helps you!