【问题标题】:Http get request is successful but no data is being renderedHttp 获取请求成功但没有数据被渲染
【发布时间】:2022-01-11 11:31:39
【问题描述】:

我正在尝试从网络服务器获取数据,它成功了,因为我可以将对象输出到控制台,但不会在组件模板中呈现。

export class CountrydetailsComponent implements OnInit {

  public country: ICountry;

  constructor(private _backendService: BackendService, private router: Router) { }

  ngOnInit(): void {
    this.getCountry(this.router.url.replace('/',''));
  }

  getCountry(country: string): void {
    this._backendService.getCountry(country).subscribe(data => {
      this.country = data;
      console.log(data);
    });
  }
}
<tbody *ngIf="country">
    <tr>
        <td>{{country.name}}</td>
        <td>{{country.population}}</td>
        <td>{{country.area}}</td>
        <td>{{country.language}}</td>
    </tr>
</tbody>

为什么只会将数据输出到控制台而不显示在实际页面上?

向控制台输出什么:

{
    "country": {
        "name": "Russia",
        "capital": "Moscow",
        "language": "Russian",
        "population": "144526636",
        "density": "8.4",
        "area": "3969100",
        "majorCities": [
            {
                "name": "Moscow",
                "population": "12506468",
                "area": "2562",
                "rank": "1"
            },
            {
                "name": "Saint Petersburg",
                "population": "5351935",
                "area": "1439",
                "rank": "2"
            },
            {
                "name": "Novosibirsk",
                "population": "1473754",
                "area": "503",
                "rank": "3"
            }
        ]
    }
}

国家界面:

import { ICity } from "./city";

export interface ICountry {
    name: string,
    capital: string,
    language: string,
    population: number,
    density: number,
    area: number,
    majorCities: ICity[]
}

【问题讨论】:

  • console.log 显示什么。你能分享一下共鸣吗
  • @Hamza { { "country": { "name": "Germany", "capital": "Berlin", "language": "German", "population": "82800000", "密度”:“232”,“面积”:“357168”,“主要城市”:[ { .... } ] } }
  • 如果您可以在问题上添加所有服务答案会很好

标签: javascript node.js angular express


【解决方案1】:

您应该访问国家/地区响应:

getCountry(country: string): void {
    this._backendService.getCountry(country).subscribe(data => {
      this.country = data.country;
      console.log(data);
    });
  }

【讨论】:

  • 它表示类型“ICountry”上不存在属性“country”。
  • 你能分享一下你的界面代码ICountry吗?
  • 哪个部分说@FabianTjernström
  • @Eduardo 更新了原帖。
  • 您的界面似乎正确。你做了@Hamza 建议的改变吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-26
  • 2020-08-30
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
  • 2020-04-07
相关资源
最近更新 更多