Generic type Subject<T> requires 1 type argument(s). - Angular

泄露秘密 提交于 2021-02-07 11:46:15

问题


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

I don't understand why?

Any kind of help would be awesome, thanks!


回答1:


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();



回答2:


Add a generic type for subject

private ngUnsubscribe: Subject<any> = new Subject();



回答3:


Install

npm install @types/googlemaps@3.39.12 --save-dev


来源:https://stackoverflow.com/questions/51096567/generic-type-subjectt-requires-1-type-arguments-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!