【问题标题】:error TS2741: Property 'ADVTITRE' is missing in type错误 TS2741:类型中缺少属性 \'ADVTITRE\'
【发布时间】:2022-11-16 00:01:21
【问题描述】:

我希望在 html 中显示变量 LABEL

JSON 文件是here

我正在尝试从 JSON 检索数据,但出现错误消息:

required in type 'Tit'.ts(2741) instrument-info.response.ts(8, 5):
'ADVTITRE' is declared here.

我真的不明白这个问题...

这是我目前的步骤。

仪器信息.response.ts

export interface Tit {
  ADVTITRE: {
    BASIQUETITRE: {
      SVM: number;
      ISIN: string;
      STOCK: string;
      LABEL: string;
      PLACE: number;
      PLACELABEL: string;
      REGR: number;
      REGRLABEL: string;
      DEVISE: string;
      COURS: {
        DATE: string;
        TIME: string;
        VALUE: number;
        HIGH: number;
        LOW: number;
        CLOTURE: number;
      };
    };
    EXIST: number;
    ...
  };
}

细节.component.ts

 details ? : Tit;
 svm: string | null = null;

 ngOnInit(): void {
    this.svm = this.activatedRoute.snapshot.paramMap.get('svm');

    if (!this.svm) {
        this.goBack();
        return;
    }
    this.getDetails();
 }

 private getDetails(): void {
    this.service.getInstrumentInfo(this.svm!).pipe(
        takeUntil(this.unsubscribe$)
    ).subscribe(res => {
        if (res.RETURNCODE === ApiResponseCodeEnum.Ok) {
            console.log(JSON.stringify(res.TIT.ADVTITRE.BASIQUETITRE));
            this.details = res.TIT.ADVTITRE.BASIQUETITRE; <--- my error 
        }
    });
 }

但是,我在控制台中检索数据

非常感谢您的帮助。

【问题讨论】:

  • this.details = res.TIT; 而不是 this.details = res.TIT.ADVTITRE.BASIQUETITRE;
  • @Mike S. 非常感谢。

标签: angular


【解决方案1】:

您需要将响应转换为接口类型,如下所示:

let apires:Tit=res as Tit;
this.detail=apires.ADVTITRE.BASIQUETITRE; 

【讨论】:

    猜你喜欢
    • 2019-10-30
    • 2021-08-26
    • 2019-06-25
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 2019-11-19
    • 2020-05-23
    相关资源
    最近更新 更多