在这个应用程序中,我从一个API中获取ID,并将它们传递到第二个API中,并试图为第二个API中的每个ID显示属性overviewList.overview.lifeTimeData.energy,但在本例中,我所能做的就是显示第一个索引的属性。如何循环遍历ID数组并显示每个ID的属性?下面是我到目前为止使用的一个实时版本,代码在components > HelloWorld.vue下面。我知道我需要遍历ID数组,但我不知道如何循环。
发布于 2020-08-06 14:33:06
你想做什么还不太清楚。
但当你做的时候
axios.get(
this.host +
this.domain +
`/site/` + ids[0] + `/overview?` +
this.apiKey,
this.config
)您需要对每个id权限运行此查询。您的方法应该更像这样:
methods: {
async getSiteDetails() {
let ids = [];
await axios.get(
this.host +
this.domain +
"/sites/list?sortProperty=name&sortOrder=ASC&" +
this.apiKey,
this.config
)
.then((...responses) => {
this.siteList = responses[0].data;
ids = this.siteIdList.concat(responses[0].data.sites.site).map(x => x.id);
})
ids.forEach(this.getOverviewList);
},
async getOverviewList(id) {
axios.get(
this.host +
this.domain +
`/site/` + id + `/overview?` +
this.apiKey,
this.config
)
.then(((...responses) => {
this.overviewList[id] = responses[0].data;
}))
.catch(errors => {
console.log(errors);
});
},
}https://stackoverflow.com/questions/63284991
复制相似问题