[英]how to loop through nested json response with vue js
I'm actually a newbie to Vue JS, so i've been having a bit of a problem looping through api responses with v-for here's my html我实际上是 Vue JS 的新手,所以我在使用 v-for 循环遍历 api 响应时遇到了一些问题,这是我的 html
Get Coins Data获取硬币数据
<div v-for="coin in coinsData">{{coinsData.data.coins.name}}</div>
my javascript:我的javascript:
var app = new Vue({
el: '#app',
data: {
coinsData: []
},
methods: {
getCoinsData() {
fetch("https://api.coinranking.com/v1/public/coins")
.then(response => response.json())
.then(data => (this.coinsData = data));
}
}
})
the response I want to loop through is at https://api.coinranking.com/v1/public/coins Its quite large so I didn't paste it in here :)我想循环的响应是在https://api.coinranking.com/v1/public/coins它非常大所以我没有把它粘贴在这里:)
Call the method in a lifecycle hook like created
.在诸如created
类的生命周期钩子中调用该方法。 And make sure you get the proper response property;并确保您获得正确的响应属性; coins
is actually 3 levels deep in the response: coins
实际上在响应中有 3 个级别:
var app = new Vue({ el: '#app', data: { coinsData: [] }, methods: { getCoinsData() { fetch("https://api.coinranking.com/v1/public/coins") .then(response => response.json()) .then(json => this.coinsData = json.data.coins); } }, created() { this.getCoinsData(); } })
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <div v-for="coin in coinsData">{{ coin.name }}</div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.