首页 > 解决方案 > 我需要从此 API 中获取“Citta”的值并将其保存到变量中,我该怎么做?

问题描述

这是 API:

0: {CodSoggetto: 22543, CognomeDenominazione: "STELLA", Nome: "ANTONIO", CF_PI: "STLNTN29S26C975F",…}
CF_PI: "STLNTN2G9S26C975F"
CNascita: "CONVERSANO"
CResidenza: "CONVERSANO"
Cap: "7054014"
Citta: "CONVERSANO"
CodSoggetto: 24543
Cognome: "STELLA"
Deceduto: "N"
Nome: "ANTONIO"
Residente: "S"
Sesso: "M"

这是我的服务:

getCitta(){

return this.http.get(`${this._host}/Contribuente`).pipe(map((res: any) => {
  this._Var = res.Citta;
  return this._Var;
}));

当我使用“this._Var”时,它显示未定义。

标签: angular

解决方案


解决方案:

getCF(): Observable<string> {

return this.http.get(`${this._host}/Contribuente`)
.pipe(
  map((res: any) => {
    if (typeof res === 'object') {
      if (res instanceof Array) {
        return res[5].Citta;
      }
     return res.Citta;
    }
  })
);

}


推荐阅读