繁体   English   中英

对象打字稿/离子3的接口

[英]Interface for object typescript/ionic 3

我有一个从api返回的json对象,并希望创建一个包含该对象包含的字段的接口。 我正在使用离子3框架。 我想要如何创建这个接口的帮助。(我很困惑:我应该为数据创建另一个接口?如果是,如何将它包含在主界面中?)对象结构如下:

{

"status": "success",

"data": [

      {

          "id": 113,

          "subject": "hello there",

          "body": "i am hisham",

          "sender": {

              "id": 51,

              "country": {

                  "id": 9,

                  "name_en": "Syria",

              }

          }

      },

      {

          "id": 114,

          "subject": "hello there",

          "body": "i am lkfdj",

          "sender": {

              "id": 54,

              "country": {

                  "id": 9,

                  "name_en": "Syria",
              }

          }

      }

  ]

}

如果要定义接口,则应为响应中的每个对象定义一个接口。 您不必,但要获得正确的类型完成,您应该。

interface Response {
  status: string;
  data: Data[];
}

interface Data {
  id: number;
  subject: string;
  body: string;
  sender: Sender;
}

interface Sender {
  id: number;
  country: Country;
}

interface Country {
  id: number;
  name_en: string;
}

暂无
暂无

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

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