9

无法删除 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: Generic type 'ComponentRef' 需要 1 个类型参数。

4

1 回答 1

28

ComponentRef是泛型类型。只需将您的代码更改为以下内容:

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

希望对你有帮助!

于 2016-07-01T04:44:30.847 回答