输入&#39; Observable <any>&#39;不能分配到&#39; StoresSummaryResults&#39;。财产&#39;数据&#39;类型&#39; Observable <any>&#39;

时间:2017-02-10 04:13:41

标签: angularjs typescript observable

我是使用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>'.

我确实有定义的数据结构,所以我不确定要纠正什么。

1 个答案:

答案 0 :(得分:0)

StoreInfo属性的输入为StoresSummaryResults,但您尝试为其分配storesService.getResults()的返回值Observable<StoresSummaryResults>

因此,要么将StoreInfo更改为Observable<StoresSummaryResults>,要么将其分配给订阅:

this.storeSevice.getResults()
    .subscribe(results => this.StoreInfo = results);