本文介绍了通用类型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个类型参数.-角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!