通用类型'ComponentRef<C>'需要 1 个类型参数 [英] Generic type 'ComponentRef<C>' requires 1 type argument(s)

查看:17
本文介绍了通用类型'ComponentRef<C>'需要 1 个类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法删除 ionic-2 中的动态组件.打字稿编译时出现异常

<块引用>

通用类型 'ComponentRef' 需要 1 个类型参数".

此外,在不使用 ionic2 的情况下使用相同的代码.非常感谢您的帮助.提前致谢.

class DynamicCmp {_ref:组件引用;_idx:号码;构造函数(私有解析器:ComponentResolver,私有位置:ViewContainerRef){}消除() {this._ref.destroy();}添加1(){this.resolver.resolveComponent(DynamicCmp).then((工厂:ComponentFactory<any>) => {让 ref = this.location.createComponent(factory, 0);ref.instance._ref = ref;ref.instance._idx = this._idx++;});}}

<块引用>

异常:TypeScript 错误:....../home/home.ts(9,11): Erro rTS2314:通用类型ComponentRef"需要 1 个类型参数.

解决方案

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

class DynamicCmp {_ref: ComponentRef<any>;<== 添加 <any>

希望对你有帮助!

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

解决方案

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

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

Hope it helps you!

这篇关于通用类型'ComponentRef&lt;C&gt;'需要 1 个类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆