请帮助修复服务:
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
@Injectable()
export class RecordsService {
constructor(private http: Http){ }
getRecords(dateBeginUnix, dateEndUnix): Object {
console.log(dateBeginUnix, dateEndUnix);
let result = this.http.get('http://127.0.0.1:8000/days/' + dateBeginUnix + '/' + dateEndUnix);
console.log('result', result);
return result;
}
}
成分:
export class PanelComponent implements OnInit {
constructor(private recordsService: RecordsService) { }
private modelDateBegin: Object;
private modelDateEnd: Object;
private records: Object;
private data: Object;
displayStatistic() {
this.recordsService
.getRecords(this.modelDateBegin['epoc'], this.modelDateBegin['epoc'])
.subscribe((data: Response) => this.records = data.json());
}
}
控制台显示遵循errormessage:
/home/kalinin/angular2/rurun/src/app/panel/panel.component.ts中的错误 (31,10):'对象'类型中不存在属性'subscribe'。
在组件modelDateBegin ['epoc']和modelDateBegin ['epoc']中包含unix datestamp。
地址:
'http://127.0.0.1:8000/days/' + dateBeginUnix + '/' + dateEndUnix
以json格式返回数据。
但是控制台显示错误消息......
答案 0 :(得分:0)
你的getRecords应该返回Observable
而不是Object
,只有Observable可以订阅而不是Object,试试这个
getRecords(dateBeginUnix, dateEndUnix): Observable<any>{
console.log(dateBeginUnix, dateEndUnix);
let result = this.http.get('http://127.0.0.1:8000/days/' + dateBeginUnix + '/' + dateEndUnix);
console.log('result', result);
return result;
}