【问题标题】:Type 'MyModel' is missing the following properties from type 'Observable<MyModel>': _isScalar, source, operator, lift, and 6 more in Angular类型“MyModel”缺少类型“Observable<MyModel>”中的以下属性:Angular 中的 _isScalar、source、operator、lift 和另外 6 个属性
【发布时间】:2020-07-09 09:57:44
【问题描述】:

我有这个 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.

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

我该如何解决这个问题?

【问题讨论】:

  • API 是否返回一个对象或对象数组?
  • 一个对象。即使出现错误,我也能够在页面上显示数据,但我想修复错误。

标签: angular rxjs observable angular9 angular2-observables


【解决方案1】:

而不是

researchContentByCompany: Observable<ResearchContentByCompany>;

如下定义,

researchContentByCompany: ResearchContentByCompany;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-25
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 2021-04-20
    • 2021-07-29
    • 1970-01-01
    相关资源
    最近更新 更多