gpt4 book ai didi

angular - 通用类型 'ComponentRef' 需要 1 个类型参数

转载 作者:太空狗 更新时间:2023-10-29 17:21:43 33 4
gpt4 key购买 nike

无法删除 ionic-2 中的动态组件。 typescript 编译时说异常

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

此外,相同的代码在不使用 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++;
});
}
}

Exception: TypeScript error: ....../home/home.ts(9,11): Erro r TS2314: Generic type 'ComponentRef' requires 1 type argument(s).

最佳答案

ComponentRef 是一个通用类型。只需更改您的代码如下:

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

希望对你有帮助!

关于angular - 通用类型 'ComponentRef<C>' 需要 1 个类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38136660/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com