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

use*_*411 6 ionic2 angular2-components angular

无法去除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++;
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

例外:TypeScript错误:....../home/home.ts(9,11):错误TS2314:通用类型'ComponentRef'需要1个类型的参数.

yur*_*zui 24

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

class DynamicCmp {
  _ref: ComponentRef<any>; <== add <any>
Run Code Online (Sandbox Code Playgroud)

希望它能帮到你!