gpt4 book ai didi

javascript - 使用 Vue.JS/Axios 和来自第三方 API(基于 Django!)的数据填充 DOM

转载 作者:行者123 更新时间:2023-11-30 19:12:15 25 4
gpt4 key购买 nike

我想使用 Vue.js 使用从第三方 API 获取的数据填充 DOM(据说我无法控制 API)。 Vue 函数调用并返回所需的数据,它还会创建正确数量的 html div。但问题是没有数据转发到那些 html/p 容器。

注意: API 数据 (JSON) 有点令人困惑,因为它是某种数组中的数组结构(我已经与提供商讨论过,他们有充分的理由来构造这个端点原样)。然而,它返回数据就好了。

尚未解决的类似问题:

Vuejs Axios data not showing

现在我真的不知道如何进行。

这是我的 Vue.js 函数:

          var app = new Vue({
el: '#Rank',
data: {
info: []
},
created() {
axios
.get("https://cors-anywhere.herokuapp.com/https://www.api-football.com/demo/api/v2/leagueTable/" + league_id)
.then(response => {
this.info = response.data.api.standings[0];
console.log(response.data.api.standings[0]);
});
}
});

这是 HTML 部分:

          <div class="table" id="Rank">
<div><p>Rank</p></div>
<div v-for="rank in info" class="rank"><p>{{ standings.rank }}</p></div>
</div>

JSON 的结构如下:

{
"api": {
"results": 1,
"standings": [
[{
"rank": 1,
"team_id": 85,
"teamName": "Paris Saint Germain",
"logo": "https://media.api-football.com/teams/85.png",
"group": "Ligue 1",
"forme": "DLWLL",
"description": "Promotion - Champions League (Group Stage)",
"all": {
"matchsPlayed": 35,
"win": 27,
"draw": 4,
"lose": 4,
"goalsFor": 98,
"goalsAgainst": 31
},
"home": {
"matchsPlayed": 18,
"win": 16,
"draw": 2,
"lose": 0,
"goalsFor": 59,
"goalsAgainst": 10
},
"away": {
"matchsPlayed": 17,
"win": 11,
"draw": 2,
"lose": 4,
"goalsFor": 39,
"goalsAgainst": 21
},
"goalsDiff": 67,
"points": 85,
"lastUpdate": "2019-05-04"
},
[...]
}
]
]
}
}

v-for 输出,它创建了正确数量的 html div,但没有任何数据......:

enter image description here

这是来自 Vue 开发工具的信息:

enter image description here

enter image description here

最佳答案

您使用了错误的 key rank in info在 v-for 中,将其重命名为 standings如果你打算使用 standings.rank

 <div class="table" id="Rank">
<div><p>Rank</p></div>
<div v-for="standings in info" class="rank"><p>{{ standings.rank }}</p></div>
</div>

更新

created() {
axios
.get("https://cors-anywhere.herokuapp.com/https://www.api-football.com/demo/api/v2/leagueTable/" + league_id)
.then(response => {
this.info = response.data.api.standings[0];
console.log('updated info', response.data.api.standings[0]); // can you share the value here ?
});
}

编辑:下面的代码运行良好,您的问题应该出在其他地方。

https://jsfiddle.net/59Lnbk17/1/

关于javascript - 使用 Vue.JS/Axios 和来自第三方 API(基于 Django!)的数据填充 DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58357065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com