【问题标题】:Generic type Subject<T> requires 1 type argument(s). - Angular通用类型 Subject<T> 需要 1 个类型参数。 - 角
【发布时间】:2018-12-08 08:15:00
【问题描述】:

我正在从我的服务中获取数据,我对订阅后退订很重要感到不满,这就是我的做法:

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

我不明白为什么?

任何形式的帮助都会很棒,谢谢!

【问题讨论】:

    标签: angular typescript rxjs angular-services


    【解决方案1】:

    为主题添加泛型类型

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

    【讨论】:

    【解决方案2】:

    Subject 类在 TypeScript 中有一个泛型类型参数。这意味着,该类的实例只能通过传递额外的类型参数来创建,如下所示:

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-05
    • 2017-10-23
    • 2020-10-26
    • 2017-12-05
    • 2017-01-09
    • 1970-01-01
    • 2017-02-08
    • 2020-12-06
    相关资源
    最近更新 更多