简体   繁体   English

如何将 map json 插入 Angular 中的接口 object

[英]How to map json into interface object in Angular

I am learning Angular.我正在学习 Angular。 (Version 11). (第 11 版)。

I'm not quite sure what I am missing.我不太确定我错过了什么。 I am calling an API from my service to get a JSON object and return it into the interface object that I created.我正在从我的服务中调用 API 以获取 JSON object 并将其返回到创建的接口 ZA8CFDE633149EB2AC96F89 中。

However when I look at the value country in the html it is not my country interface it is the full object that came from the api.但是,当我查看 html 中的值国家/地区时,它不是我的国家/地区界面,而是来自 api 的完整 object。 (My interface should only have a subset of values). (我的界面应该只有一个值的子集)。

Service:服务:

  getCountry(): void {
    const name = this.route.snapshot.paramMap.get('name');
    this.countryService.getCountry(name)
      .subscribe(x => this.country = x);
  };

Component:零件:

export class CountryDetailComponent implements OnInit {
  country: Country;
  
  constructor(
    private countryService: CountryService,
    private route: ActivatedRoute
  ) { }

  ngOnInit(): void {
    this.getCountry();
    const x = this.country;
  }

  getCountry(): void {
    const name = this.route.snapshot.paramMap.get('name');
    this.countryService.getCountry(name)
      .subscribe(x => this.country = x);
  };

Country:国家:

export interface Country {
  name: string,
  alpha2Code: string,
  alpha3Code: string,
  capital: string,
  region: string,
  subregion: string,
  population: number,
  demonym: string,
  area: number,
  gini: number
  flag: string
}

component.html组件.html

{{country | json}}

API: https://restcountries.eu/rest/v2/name/aruba?fullText=true API: https://restcountries.eu/rest/v2/name/aruba?fullText=true

Result from html: html 的结果:

[ { "name": "Aruba", "topLevelDomain": [ ".aw" ], "alpha2Code": "AW", "alpha3Code": "ABW", "callingCodes": [ "297" ], "capital": "Oranjestad", "altSpellings": [ "AW" ], "region": "Americas", "subregion": "Caribbean", "population": 107394, "latlng": [ 12.5, -69.96666666 ], "demonym": "Aruban", "area": 180, "gini": null, "timezones": [ "UTC-04:00" ], "borders": [], "nativeName": "Aruba", "numericCode": "533", "currencies": [ { "code": "AWG", "name": "Aruban florin", "symbol": "ƒ" } ], "languages": [ { "iso639_1": "nl", "iso639_2": "nld", "name": "Dutch", "nativeName": "Nederlands" }, { "iso639_1": "pa", "iso639_2": "pan", "name": "(Eastern) Punjabi", "nativeName": "ਪੰਜਾਬੀ" } ], "translations": { "de": "Aruba", "es": "Aruba", "fr": "Aruba", "ja": "アルバ", "it": "Aruba", "br": "Aruba", "pt": "Aruba", "nl": "Aruba", "hr": "Aruba", "fa": "آروبا" }, "flag": "https://restcountries.eu/data/abw.svg", "regionalBlocs": [], "cioc": "ARU" } ]

You're missing few fundamental concepts:您缺少一些基本概念:

  1. The API returns an Array of countries( with only one value) so the variable to hold this value should be: API 返回一个国家数组(只有一个值),所以保存这个值的变量应该是:

    this.countryService.getCountry(name).subscribe(x => this.country = x[0]);

  2. Interfaces are used for static type checking, at run time there is no interface because the code gets transpiled into Javascript and interface doesn't exist in JS.接口用于 static 类型检查,在运行时没有接口,因为代码被转换为 Javascript 并且接口在 JS 中不存在。 Interface is not meant to be used like the way you're using. Interface并不意味着像您使用的方式那样使用。

here's a good read: https://www.typescriptlang.org/docs/handbook/interfaces.html这是一个很好的阅读: https://www.typescriptlang.org/docs/handbook/interfaces.html

  1. You need to create a class and assign the values manually if you want to keep subset of values.如果要保留值的子集,则需要创建 class 并手动分配值。
 name: string;
 alpha2Code: string;
 alpha3Code: string;
 capital: string;
 region: string;
 subregion: string;
 population: number;
 demonym: string;
 area: number;
 gini: number;
 flag: string;

 constructor(d: any) {
   this.name = d.name;
   ...
 }
}

If you wish to strip out your additional properties, you need a class .如果你想去掉你的额外属性,你需要一个class An interface doesn't actually do anything. interface实际上并没有做任何事情。 It's strictly a data contract or schema for a model.它严格来说是 model 的数据协定或模式。 If you set something and there are additional properties, they will be set on the interface.如果您设置了一些东西并且有其他属性,它们将在界面上设置。

TypeScript Playground TypeScript 游乐场

class Country {
  name: string;
  alpha2Code: string;
  alpha3Code: string;
  capital: string;
  region: string;
  subregion: string;
  population: number;
  demonym: string;
  area: number;
  gini: number;
  flag: string;

  constructor(d: any) {
    this.name = d.name;
    this.alpha2Code = d.alpha2Code;
    ...
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM