如何从一个公共方法中提取变量的值并调用另一个方法

时间:2019-07-16 14:48:24

标签: javascript angular typescript observable ionic4

我有一个StepsCount方法,其中正在获取一系列时间戳数据。我想将timeSeries调用到另一个组件中,我继承了此类方法,但是我不确定如何从该方法中获取数据(timeSeries)。

//Main method where data sits in timeSeries 
public StepsCount(): Observable<any> {
if (this.patientIdentifierCache !== undefined) {
  this.getAllObservations(this.patientIdentifierCache, function(
    allObservations
  ) {

    timeSeries = allObservations.temperature;
    const filter = new DateFilter();
    filter.datePoint = new Date(2005, 9, 10);

    timeSeries = timeSeries.filter(filter.getDateFilter());

    return timeSeries;
  });

}
return Observable.of( timeSeries);

}

//Second method where i am trying to reach timeSeries  
const service = new MiddlewareApi();
service.StepsCount().subscribe((timeSeries => console.log (timeSeries))); 
//timeSeires variable is in ^^^^^ this method StepsCount 

//This second method should return value of timeSeries into new method.

1 个答案:

答案 0 :(得分:0)

您必须更深入地了解RxJ和如何订阅Observable。 我看到了一些服务和缓存内容的滥用,无论如何,您的服务方法StepsCount应该返回一个Observable,而使用者(希望是一个组件)应该通过构造函数注入服务,并执行类似的操作

service.StepsCount().subscribe(timeSeries => console.log(timeSeries));