Javascript 使用Vue.JS/Axios和来自第三方API(基于Django!)的数据填充DOM
我想使用Vue.js用从第三方API获取的数据填充DOM(也就是说我对API没有控制权)。Vue函数调用并返回所需的数据,它还创建了正确数量的html div。但问题是没有数据转发到这些html/p容器 注意:API数据(JSON)有点混乱,因为它是数组结构中的某种数组(我已经和提供者谈过了,他们有一些很好的理由按原样构造这个端点)。但是,它返回的数据很好 类似问题尚未解决:Javascript 使用Vue.JS/Axios和来自第三方API(基于Django!)的数据填充DOM,javascript,vue.js,axios,Javascript,Vue.js,Axios,我想使用Vue.js用从第三方API获取的数据填充DOM(也就是说我对API没有控制权)。Vue函数调用并返回所需的数据,它还创建了正确数量的html div。但问题是没有数据转发到这些html/p容器 注意:API数据(JSON)有点混乱,因为它是数组结构中的某种数组(我已经和提供者谈过了,他们有一些很好的理由按原样构造这个端点)。但是,它返回的数据很好 类似问题尚未解决: var app = new Vue({ el: '#Rank',
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]);
});
}
});
<div class="table" id="Rank">
<div><p>Rank</p></div>
<div v-for="rank in info" class="rank"><p>{{ standings.rank }}</p></div>
</div>
{
"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"
},
[...]
}
]
]
}
}
var app = new Vue({
delimiters : ['[[',']]'],
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]);
});
}
});
<div id="app">
<div class="table" id="Rank">
<div><p>Rank</p></div>
<div v-for="standings in info" class="rank"><p>[[ standings.rank ]]</p></div>
</div></div>
现在我真的不知道该怎么做
这是我的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]);
});
}
});
<div class="table" id="Rank">
<div><p>Rank</p></div>
<div v-for="rank in info" class="rank"><p>{{ standings.rank }}</p></div>
</div>
{
"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"
},
[...]
}
]
]
}
}
var app = new Vue({
delimiters : ['[[',']]'],
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]);
});
}
});
<div id="app">
<div class="table" id="Rank">
<div><p>Rank</p></div>
<div v-for="standings in info" class="rank"><p>[[ standings.rank ]]</p></div>
</div></div>
这是HTML部分:
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]);
});
}
});
<div class="table" id="Rank">
<div><p>Rank</p></div>
<div v-for="rank in info" class="rank"><p>{{ standings.rank }}</p></div>
</div>
{
"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"
},
[...]
}
]
]
}
}
var app = new Vue({
delimiters : ['[[',']]'],
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]);
});
}
});
<div id="app">
<div class="table" id="Rank">
<div><p>Rank</p></div>
<div v-for="standings in info" class="rank"><p>[[ standings.rank ]]</p></div>
</div></div>
和v-for
输出,它创建了正确数量的html div,但没有任何数据..:
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]);
});
}
});
<div class="table" id="Rank">
<div><p>Rank</p></div>
<div v-for="rank in info" class="rank"><p>{{ standings.rank }}</p></div>
</div>
{
"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"
},
[...]
}
]
]
}
}
var app = new Vue({
delimiters : ['[[',']]'],
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]);
});
}
});
<div id="app">
<div class="table" id="Rank">
<div><p>Rank</p></div>
<div v-for="standings in info" class="rank"><p>[[ standings.rank ]]</p></div>
</div></div>
这是来自Vue开发工具的信息:
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]);
});
}
});
<div class="table" id="Rank">
<div><p>Rank</p></div>
<div v-for="rank in info" class="rank"><p>{{ standings.rank }}</p></div>
</div>
{
"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"
},
[...]
}
]
]
}
}
var app = new Vue({
delimiters : ['[[',']]'],
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]);
});
}
});
<div id="app">
<div class="table" id="Rank">
<div><p>Rank</p></div>
<div v-for="standings in info" class="rank"><p>[[ standings.rank ]]</p></div>
</div></div>
您在v-for中使用了错误的键
信息排名
,如果要使用排名,请将其重命名为排名。排名
<div class="table" id="Rank">
<div><p>Rank</p></div>
<div v-for="standings in info" class="rank"><p>{{ standings.rank }}</p></div>
</div>
编辑:
下面的代码运行良好,您的问题应该在别处
这就是最终对我起作用的原因:
将括号从花括号改为其他任何形式,这样Django就不会对其变量产生腐蚀,Django也使用花括号
根据初始问题的工作解决方案:
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]);
});
}
});
<div class="table" id="Rank">
<div><p>Rank</p></div>
<div v-for="rank in info" class="rank"><p>{{ standings.rank }}</p></div>
</div>
{
"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"
},
[...]
}
]
]
}
}
var app = new Vue({
delimiters : ['[[',']]'],
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]);
});
}
});
<div id="app">
<div class="table" id="Rank">
<div><p>Rank</p></div>
<div v-for="standings in info" class="rank"><p>[[ standings.rank ]]</p></div>
</div></div>
HTML:
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]);
});
}
});
<div class="table" id="Rank">
<div><p>Rank</p></div>
<div v-for="rank in info" class="rank"><p>{{ standings.rank }}</p></div>
</div>
{
"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"
},
[...]
}
]
]
}
}
var app = new Vue({
delimiters : ['[[',']]'],
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]);
});
}
});
<div id="app">
<div class="table" id="Rank">
<div><p>Rank</p></div>
<div v-for="standings in info" class="rank"><p>[[ standings.rank ]]</p></div>
</div></div>
等级
[[standings.rank]]
这看起来与您以前提出的问题非常相似。您说divs中没有数据,但在您发布的图片中,您没有扩展任何div。@谢谢您的帖子。我将替换图像,但只有空的元素。我过去也有过类似的问题,是的。如果答案是相同的,我可以再次删除这个。@Phanti我已经在这里解决了,请检查答案谢谢。我用你的建议代替了它,但仍然没有数据。我猜这是由于“双数组”结构造成的?Phanti,你能分享api json的实际结构吗?还有更多的例子这是json url:你可以使用lint(你可能知道,lol),让我知道我是否应该在我的初始帖子中发布整个回调。感谢您可以检查更新并在response.data.api.standings[0]
中共享值吗?如果将this.info
设置为该值,那么嵌套结构应该不会有问题,因为info将是一个简单的数组