【发布时间】:2019-01-18 14:51:45
【问题描述】:
我正在使用 Vue.JS 制作动态下拉列表,Countries 和 Regions 正在动态获取,但 Cities JSON 已收到但未提取到视图中,并且 在控制台,我找不到我的错误。
HTML
<div id="app">
<form action="">
<div class="form-group">
<label for="country"></label>
<select v-model="country" name="country" class="form-control" @change="loadRegions">
<option>Select country</option>
<option v-for="country in countries" :value="country.id">@{{ country.name }}</option>
</select>
</div>
<div class="form-group">
<label for="region"></label>
<select v-model="region" name="region" class="form-control" @change="loadCities">
<option>Select region</option>
<option v-for="region in regions" :value="region.id">@{{ region.name }}</option>
</select>
</div>
<div class="form-group">
<label for="city"></label>
<select v-model="city" name="city" class="form-control">
<option>Select city</option>
<option v-for="city in cities" :value="city.id">@{{ city.name }}</option>
</select>
</div>
</form>
</div>
JS
<script type="text/javascript">
var app = new Vue({
el: '#app',
data() {
return {
countries: [],
regions: [],
cities: [],
country:'',
region:'',
city:'',
}
},
mounted() {
this.loadCountries();
},
methods: {
loadCountries() {
axios.get('/countries')
.then(response => this.countries = response.data.countries)
.catch(error => console.log(error))
},
loadRegions() {
axios.get('/regions', {params: {country: this.country}})
.then(response => this.regions = response.data.regions)
.catch(error => console.log(error))
},
loadCities() {
axios.get('/cities', {params: {country: this.country, region: this.region}})
.then(response => this.cities = response.data.cities)
.catch(error => console.log(error))
}
}
})
</script>
【问题讨论】:
-
axios 会有任何错误。检查响应?
-
实际上 (localhost:8000/cities?country=n®ion=n) 正在返回对象,但在 Vue Devtool 中它返回的是空数组
-
没有错误?在控制器中?
-
我在 axios 传递多个参数时遇到了问题,可能是相关的。我已经停止使用它,因为那里的人对社区没有任何了解,例如:github.com/axios/axios/issues/362#issuecomment-401014489 两年的错误,没有人关心...
-
哦,所以有错误......它在 axios 中。也许你的参数。