【发布时间】:2020-12-13 06:49:04
【问题描述】:
我有一张用户资料卡,它在方框中显示不同的测量值,例如温度、体积和重量。
目前我正在从 axios GET 请求中获取每个用户的名称,然后我需要获取该数据中的 ID 并使用 ID 执行另一个 GET 请求,该 ID 为我提供了性能数据,在我得到的数据中一个性能 ID,我需要使用它来获取最终的测量数据数组。
我想知道如何仅为具有性能 ID 的用户发出最后一个 GET 请求,然后获取测量数据并显示在每个测量框的每个用户配置文件(具有测量数据)中?
看起来像这样:
- 我在 vuex 中发出第一个 axios 请求,并使用 v-for 显示每个用户名:
GET api/profiles返回:
"data": [
{
"id": 1,
"name": "Joe",
},
{
"id": 2,
"name": "Max",
},
<div class="outer" v-for="(item, index) in profiles" :key="item.id" >
<p> User Name: {{ item.name }}</p>
- 然后我需要发出另一个 GET 请求以获得性能:
GET api/performance/{profileID}只为拥有(其他用户返回空数据对象)的用户返回:
mounted() {
this.$store.state.profiles.forEach((item, index) => {
axios.get('api/performance/' + this.profile[index].id)
.then(response => {
this.performanceId = response.data.data.id
})
});
}
"data": {
"id": 1,
"profile_id": 2,
.....other keys and values
}
- 最后,对于有性能 ID 的用户,我需要获取测量结果:
GET api/measurements/{profileID}/{performanceID}返回:
"data": {
"profileID": "2",
"perfomanceID": "1",
"data": [
{
"key": "temp",
"data": "37",
},
{
"key": "vol",
"data": "22",
},
{
"key": "weight",
"data": "200",
},
然后我希望每个数据点都显示在框中:
<div class="outer" v-for="(item, index) in profiles" :key="item.id" >
<p> User Name: {{ item.name }}</p>
<div class="box 1">Temperature: </div>
<div class="box 2">Volume: </div>
<div class="box 3">Weight: </div>
</div>
It would like this for Max's profile:
User Name: Max
Temperature: 37
Volume: 22
Weight: 200
任何帮助将不胜感激,谢谢!
【问题讨论】:
标签: javascript json vue.js get axios