RXJS ERROR TypeError: "this._subscribe is not a function"

RXJS ERROR TypeError: "this._subscribe is not a function"

我收到以下错误,但无法解决。

ERROR TypeError: "this._subscribe is not a function"

Here 也是图像形式的完整堆栈跟踪。

我努力实现的目标 我有一个名为 ChargingStation 的 class,它有几个属性,看起来像 this. The instances of this class should be fetched from my api. This functionality is delivered by a class called ChargingStationService. The corresponding method inside the ChargingStationService class is shown here. Currently I just try to deliver a mocked Observable back. The getChargingStation method will be called from my ChargingStationComponent that looks like this。检索到的 ChargingStation 对象应该通过数据绑定显示在组件视图中。但是 DataBinding ist 不工作/没有显示。它曾经与服务 return 一起工作,只是一个 ChargingStation 类型的对象,而不是 Observable

申请流程是怎样的

ChargingStationComponentngOnInit 方法中,它获取 url / 中指定的 ID充电站/{id}

  ngOnInit() {
    this.getChargingStation(+this.route.snapshot.paramMap.get('id'));
  }

使用此 ID 调用 getChargingStation(id) 方法。

  getChargingStation(id: number): void {
    console.log(this.chargingStationService.getChargingStation(id));
    this.chargingStationService.getChargingStation(id)
      .subscribe(chargingStation => (this.chargingStation = chargingStation));
  }

此方法在内部使用注入的 ChargingStationService,它将在将来从 API 中检索 ChargingStations。目前这个方法看起来像这样并且应该 return 一个充电站的模拟观察。

 getChargingStation(id: number): Observable<ChargingStation> {
    let searchedChargingStation= new ChargingStation(
      id,
      false,
      0.37,
      this.plugs,
      0.0,
      0.0,
      "Store Street, R802"
    );
    let observableSearchedChargingStation = Observable.create(searchedChargingStation);
    console.log(observableSearchedChargingStation);
    return observableSearchedChargingStation;
  }

我认为是错误的 我认为 Observable.create 方法可能是错误的选择,但我还没有找到更合适的方法。

希望你们中的一些人能帮助我:)非常感谢,圣诞快乐

您好 一月

您不会在创建新值后将其推送给您的观察者:)

而不是

let observableSearchedChargingStation = Observable.create(searchedChargingStation);

做:

Observable.create((observer) => {
    observer.next(searchedChargingStation);
});

并且您的代码应该按原样工作。