本文介绍了通用类型 Subject <T>需要 1 个类型参数.- 角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从我的服务中获取数据,并且我已经意识到在订阅后取消订阅很重要,我是这样做的:
I'm getting data from my service and I've red about it's important to unsubscribe after subscribe, and here is how I did it:
export class RItemComponent implements OnInit {
apiPath = environment.apiUrl;
private ngUnsubscribe: Subject = new Subject();
constructor(private _sharedService: SharedService) { }
rItems: Product[];
ngOnInit() {
this._sharedService.
getReceiptItem().takeUntil(this.ngUnsubscribe).
subscribe(products => this.rItems = products);
}
ngOnDestroy() {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}
}
但现在我收到一个错误:
But now I'm getting an error:
Generic type Subject<T> requires 1 type argument(s). subscribe
我不明白为什么?
任何形式的帮助都会很棒,谢谢!
Any kind of help would be awesome, thanks!
推荐答案
Subject
类在 TypeScript 中有一个泛型类型参数.这意味着,此类的实例只能通过传递额外的类型参数来创建,如下所示:
The Subject
class has a generic type parameter in TypeScript. This means, that instances of this class can only be created by passing an additional type parameter, like this:
private ngUnsubscribe: Subject<MyClass> = new Subject();
这篇关于通用类型 Subject <T>需要 1 个类型参数.- 角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!