【发布时间】:2021-12-30 12:58:52
【问题描述】:
我正在学习 v-for 循环如何在 Vue 中工作。非常喜欢我可以直接在模板HTML 中运行循环但不确定如何正确使用 v-for 循环来深入研究多级数组的想法。
我有一个名为 playerDataList 的变量,它包含一些 JSON 数据。下面是一个示例
"stats" : [ {
"type" : {
"displayName" : "yearByYear",
"gameType" : null
},
"splits" : [ {
"season" : "20052006",
"stat" : {
"assists" : 43,
"goals" : 49,
"pim" : 82,
"games" : 62,
"penaltyMinutes" : "82",
"faceOffPct" : 0.0,
"points" : 92
},
"team" : {
"name" : "Lon. Jr. Knights",
"link" : "/api/v1/teams/null"
},
"league" : {
"name" : "Minor-ON",
"link" : "/api/v1/league/null"
},
"sequenceNumber" : 1
}, {
"season" : "20062007",
"stat" : {
"assists" : 15,
"goals" : 7,
"pim" : 30,
"games" : 62,
"powerPlayGoals" : 0,
"penaltyMinutes" : "30",
"faceOffPct" : 0.0,
"shortHandedGoals" : 0,
"plusMinus" : 11,
"points" : 22
},
"team" : {
"id" : 1874,
"name" : "Kitchener",
"link" : "/api/v1/teams/1874"
},
"league" : {
"id" : 141,
"name" : "OHL",
"link" : "/api/v1/league/141"
},
"sequenceNumber" : 1
}, {
"season" : "20072008",
"stat" : {
"assists" : 40,
"goals" : 25,
"pim" : 57,
"games" : 68,
"powerPlayGoals" : 10,
"penaltyMinutes" : "57",
"shortHandedGoals" : 0,
"plusMinus" : 9,
"points" : 65
},
"team" : {
"id" : 1874,
"name" : "Kitchener",
"link" : "/api/v1/teams/1874"
},
"league" : {
"id" : 141,
"name" : "OHL",
"link" : "/api/v1/league/141"
},
"sequenceNumber" : 1
}
}]
到目前为止,我已经得到了这段代码,它可以显示我的内容,但它只是第一个实例。它实际上并没有循环并给我每次出现。
<div class="player-stats-card-inner">
<p class="close" v-on:click='showPlayers = !showPlayers'>Close</p>
<table>
<th>
<td>Goals</td>
<td>Assists</td>
</th>
<!-- Loop through the JSON data -->
<tr v-for="stats in playerDataList.stats" :key="stats.id">
<td>
{{stats.splits[0].stat.goals}}
</td>
<td>
{{stats.splits[0].stat.assists}}
</td>
</tr>
</table>
</div>
我可以做些什么不同的事情来让这个正确循环吗?
【问题讨论】:
-
项目中没有
id,所以使用v-for="(stats, index) in playerDataList.stats" :key="index",然后如果你想循环拆分,添加另一个for循环并执行v-for="(stat, splits_index) in stats.splits" :key="splits_index",然后是{{stat.goals}}等。只有 2 个基本循环
标签: javascript arrays vue.js