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.
Below the code for example:
//import {Component} from ‘@angular/core’;
import { Component, ViewContainerRef, ElementRef, ComponentRef, ComponentResolver, ViewChild, ComponentFactory } from ‘@angular/core’;
import {NavController} from ‘ionic-angular’;
@Component({
templateUrl: ‘build/pages/home/DynamicComponent.html’
})
class DynamicCmp {
_ref: ComponentRef;
_idx: number;
constructor(private resolver: ComponentResolver, private location: ViewContainerRef) { }
remove() {
//this._ref.dispose();
this._ref.destroy();
}
add1() {
this.resolver.resolveComponent(DynamicCmp).then((factory: ComponentFactory<any>) => {
let ref = this.location.createComponent(factory, 0);
// this._dcl.loadNextToLocation(DynamicCmp, this._e).then((ref) => {
ref.instance._ref = ref;
ref.instance._idx = this._idx++;
});
}
}
@Component({
templateUrl: ‘build/pages/home/home.html’
})
export class HomePage {
constructor(private navController: NavController, private resolver: ComponentResolver) {}
idx: number = 0;
@ViewChild('location', { read: ViewContainerRef}) location: ViewContainerRef;
add() {
this.resolver.resolveComponent(DynamicCmp).then((factory: ComponentFactory<any>) => {
let ref = this.location.createComponent(factory)
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).
Much appreciate your help. Thanks in Advance.