首页 > 解决方案 > “MyModel”类型缺少“Observable”类型的以下属性':Angular 中的 _isScalar、source、operator、lift 等 6 个

问题描述

我有这个 TS 模型:

export interface ResearchContentByCompany {
    companyId: number;
    companyOverview: string;
    industryAnalysis: string;
    qualityAssessment: string;
    financials: string;
    valuations: string;
}

在服务中,这是我为获取数据而进行的 api get 调用:

public getResearchSectionContentByCompany(companyId) {
        return this.http.get<ResearchContentByCompany>("../api/contenteditor/getresearchcontentbycompany?companyId=" + companyId, {
            headers: this.headers,
        }).pipe(map(data => {
            return data;
        }));
    }

在组件中我声明:

 researchContentByCompany: Observable<ResearchContentByCompany>;

在 ngOnInit 方法中,我这样做:

this.contentEditorService.getResearchSectionContentByCompany(this.DataResult.companyId).subscribe(result => {
                                this.researchContentByCompany = result;
                            });

我能够按预期从 API 获取数据,但出现此错误:

    Type 'ResearchContentByCompany' is missing the following properties from type 
'Observable<ResearchContentByCompany>': _isScalar, source, operator, lift, and 6 more.

当我执行 this.researchContentByCompany[0] = result 时,我在 get 调用中注意到错误消失了,但是数据丢失了,因为这不是一个数组,而只是一个具有属性的对象。

我该如何解决这个问题?

标签: angularrxjsobservableangular9angular2-observables

解决方案


代替

researchContentByCompany: Observable<ResearchContentByCompany>;

定义如下,

researchContentByCompany: ResearchContentByCompany;

因为结果只是ResearchContentByCompany对象的类型。


推荐阅读