javascript - 对象 typescript/ionic 3 的接口(interface)

标签 javascript typescript ionic-framework ionic3

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

{

"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),则应为响应中的每个对象定义一个接口(interface)。您不必这样做,但要获得正确的类型完成,您应该这样做。

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;
}

关于javascript - 对象 typescript/ionic 3 的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50866790/

相关文章:

javascript - 选择所有内容可编辑的 div

javascript - d3.js - 如何在弹出窗口中显示链接相关数据(例如源和目标)

javascript - 为每个 Iframe 生成唯一的 Id,或从 DOM 节点构建 Iframe

mysql - 有关移动应用中的 Firebase 和 MySQL 的问题

javascript - 按 Javascript 数组中的出现次数(计数)排序

typescript :返回 `instance type` 的 `typeof class`

typescript - 如何在垫卡上使用角度 Material 分页?

typescript - 使用 VS Code 调试在 Docker 容器中使用 ts-node 运行的 TypeScript 应用程序时,如何正确设置断点?

vue.js - ion-icon 在 ionic vue 中不显示图标

angular - 如何在 ionic android 中将我的应用程序的目标 API 级别从 29 更改为 30