【问题标题】:Access nested api data (vue)访问嵌套的 api 数据 (vue)
【发布时间】:2022-01-08 17:36:26
【问题描述】:

我无法通过我的 api 调用访问嵌套数据。 JSON 响应如下所示:

我尝试访问属性中的属性名称。以及语言中的语言名称。 我可以使用以下代码显示 location_name {{ userdetails.location.location_name }}


我总是遇到嵌套 api 数据的问题。是否有指南如何从 api 访问数据?

谢谢!

更新:我的代码如下所示

data() {
    return {
      userdetails: undefined,
    };
  },

  methods: {
    getUserData() {
      DataService.getUserById()
        .then((response) => {
          this.userdetails = response.data;
          console.log(response);
        })
        .catch((error) => {
          console.log(
            "Ein Fehler beim User ist aufgetreten: " + error.response
          );
        });
    },
  },
  created() {
    this.getUserData();
  },
};

【问题讨论】:

  • 响应保存在任何变量中?我假设您将res.data 保存在userdetails 中?你能给我们看看代码吗?
  • @MuXeD 我将代码添加到问题中

标签: json api vue.js nested


【解决方案1】:

attributes 属性是数组所以试试(index 是数组中的索引):

{{ userdetails.attributes[index].attribute_name }}

如果你想全部展示:

<div v-for="(attr, i) in userdetails.attributes" :key="i" >
  {{ attr.attribute_name }}
</div>

【讨论】:

  • 谢谢!我不得不添加另一个“属性”,但它现在可以完美运行。
    {{ attr.attribute.attribute_name }}
  • 很好,干杯:)
猜你喜欢
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-24
  • 2016-12-04
  • 1970-01-01
  • 2019-10-08
相关资源
最近更新 更多