typescript 泛型类型 Subject<T> 需要 1 个类型参数。- 角
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51096567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generic type Subject<T> requires 1 type argument(s). - Angular
提问by Roxy'Pro
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 requires 1 type argument(s). subscribe
通用类型主题需要 1 个类型参数。订阅
I don't understand why?
我不明白为什么?
Any kind of help would be awesome, thanks!
任何形式的帮助都会很棒,谢谢!
回答by Sachila Ranawaka
Add a generic type for subject
为主题添加通用类型
private ngUnsubscribe: Subject<any> = new Subject();
回答by ggradnig
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:
该Subject
班有一个打字稿泛型类型参数。这意味着,此类的实例只能通过传递额外的类型参数来创建,如下所示:
private ngUnsubscribe: Subject<MyClass> = new Subject();