【发布时间】:2020-01-05 15:01:16
【问题描述】:
我在 visualstudiocode 中使用 vue.js 和 axios 原则上我对编码非常陌生,所以如果我的问题措辞不当,我很抱歉。我有两个从这个 API 组装的数组: 'https://docs.openaq.org/#api-_'
一个是国家列表,包含该地区有多少个城市等数据,另一个是这些国家内的城市列表以及相应数据。我使用 v-for 循环在表格中显示国家列表,然后在同一行中显示相应的按钮。
我想要发生的是,当为数组中的每个相应国家/地区单击此按钮时,将显示该国家/地区内的所有城市。有人告诉我这需要单独的 v-for 循环,但我不知道如何写出来。
<template>
<div>
<table>
<tr>
<th>Countries</th>
<th>Cities</th>
</tr>
<tr>
<td>
<ul>
<li v-for="countries in listofcountries" :key="countries.name">
{{countries.name}}
</li>
</ul>
</td>
<ul v-for="countries in listofcountries" :key="countries.name">
<li v-for="cities in lifeofcities" :key="cities.city"></li>
<button>{{countries.name}}</button>
</li>
</ul>
</tr>
</table>
</div>
</template>
<script>
export default {
name: 'HelloWorld',
data () {
return {
listofcountries: [],
listofcities: [],
}
},
mounted () {
const axios = require('axios');
var self = this;
// Make a request for a user with a given ID
axios.get('https://api.openaq.org/v1/cities')
.then(function (response) {
// handle success
console.log(response.data.results);
self.listofcities=response.data.results;
})
.catch(function (error) {
// handle error
console.log(error);
})
.finally(function () {
// always executed
});
axios.get('https://api.openaq.org/v1/countries')
.then(function (response) {
// handle success
console.log(response.data.results) ;
self.listofcountries=response.data.results;
})
.catch(function (error) {
// handle error
console.log(error);
})
.finally(function () {
// always executed
});
},
methods: {
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style>
ul {
list-style-type: none;
}
table,th,td,tr {
border: 1px solid black;
}
</style>
【问题讨论】: