通用类型Subject <t>需要1个类型参数。 -角度

时间:2018-06-29 07:34:07

标签: angular typescript rxjs angular-services

我从服务中获取数据,我对订阅后取消订阅很重要,这是我的操作方式:

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

但是现在我遇到了一个错误:

  

泛型类型Subject需要1个类型参数。订阅

我不明白为什么?

任何帮助都会很棒,谢谢!

3 个答案:

答案 0 :(得分:4)

为主题添加通用类型

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

答案 1 :(得分:4)

Subject类在TypeScript中具有通用类型参数。这意味着只能通过传递附加的类型参数来创建此类的实例,例如:

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

答案 2 :(得分:-2)

安装

npm install @types/googlemaps@3.39.12 --save-dev
相关问题