我是使用Obularable和Angular 2的新手,我的结构将不会收到结果,即使我可以验证来自REST API的响应。
我在Typescript中有以下数据类。
import { RESTResult } from '../common/RESTResult'; // Common Response Structure
import { StoresSummaryData } from './StoresSummaryData'; // Structure of records being returned as an array
export class StoresSummaryResults extends RESTResult {
Data: Array<StoresSummaryData>; // Data[] is array ofStoresSummaryData structure ({ field1, field2 ...)
constructor() {
super();
this.Data = new Array<StoresSummaryData>(); // Ensure 1 records exists for testing
this.Data.push(new StoresSummaryData(0, 0));
}
}
从REST API
中检索结果getResults(): Observable<StoresSummaryResults> { // Structure
return this.http.get(this.RequestHttpPath)
.map(this.extractData)
.catch(this.handleError);
};
private extractData(res: Response) {
let body = res.json();
return body.data || {};
}
StoreInfo: StoresSummaryResults;
public constructor(private storesService: StoresService) { }
showResults(): void {
this.StoreInfo = this.storesService.getResults();
}
我收到错误:
Typescript Error
Type 'Observable<StoresSummaryResults>' is not assignable to type 'StoresSummaryResults'. Property 'Data' is missing in type 'Observable<StoresSummaryResults>'.
我确实有定义的数据结构,所以我不确定要纠正什么。
答案 0 :(得分:0)
StoreInfo
属性的输入为StoresSummaryResults
,但您尝试为其分配storesService.getResults()
的返回值Observable<StoresSummaryResults>
。
因此,要么将StoreInfo
更改为Observable<StoresSummaryResults>
,要么将其分配给订阅:
this.storeSevice.getResults()
.subscribe(results => this.StoreInfo = results);