通用类型 Subject<T> 需要 1 个类型参数。 - Angular
Generic type Subject<T> requires 1 type argument(s). - Angular
我正在从我的服务中获取数据,我很担心订阅后取消订阅很重要,我是这样做的:
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();
}
}
但现在我收到一个错误:
Generic type Subject<T> requires 1 type argument(s). subscribe
我不明白为什么?
任何形式的帮助都会很棒,谢谢!
为主题添加通用类型
private ngUnsubscribe: Subject<any> = new Subject();
Subject
class 在 TypeScript 中有一个泛型类型参数。这意味着,这个 class 的实例只能通过传递一个额外的类型参数来创建,就像这样:
private ngUnsubscribe: Subject<MyClass> = new Subject();
我正在从我的服务中获取数据,我很担心订阅后取消订阅很重要,我是这样做的:
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();
}
}
但现在我收到一个错误:
Generic type Subject<T> requires 1 type argument(s). subscribe
我不明白为什么?
任何形式的帮助都会很棒,谢谢!
为主题添加通用类型
private ngUnsubscribe: Subject<any> = new Subject();
Subject
class 在 TypeScript 中有一个泛型类型参数。这意味着,这个 class 的实例只能通过传递一个额外的类型参数来创建,就像这样:
private ngUnsubscribe: Subject<MyClass> = new Subject();