【发布时间】:2019-07-23 23:51:41
【问题描述】:
更新 我是如何做到的如下(滚动到我的答案或这里的链接): https://stackoverflow.com/a/55013081/1729405
我正在尝试在 Angular 7 中构建 APIService。我正在使用订阅推送到无法通过其他服务访问的数组。
api服务方法如下:
getResponse(url):Observable<any> {
return this.http.post(url, '', {responseType: 'text'});
}
getAll() {
this.getResponse(this.todayApiUrl).subscribe(
(response) => {
this.data.push(response);
});
return this.data;
}
这是我尝试在辅助服务中调用它的方式:
export class AdsHealthService {
today = this.callResponseApiService.getAll();
constructor(private callResponseApiService: CallResponseApiService) { }
getHealthValue(interval, type) {
let obj = this.today;
console.log(obj);
}
}
在我console.log() 回复时得到的图片。它看起来像一个数组,但是当我尝试访问第一个元素时,我收到了 undefined 消息。我究竟做错了什么??
【问题讨论】:
标签: angular typescript api service