【问题标题】:VueJS dynamic dropdown - json data not showing in the viewVueJS 动态下拉列表 - 视图中未显示 json 数据
【发布时间】:2019-01-18 14:51:45
【问题描述】:

我正在使用 Vue.JS 制作动态下拉列表,CountriesRegions 正在动态获取,但 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>

Json object in console

Devtools screenshot

【问题讨论】:

  • axios 会有任何错误。检查响应?
  • 实际上 (localhost:8000/cities?country=n&region=n) 正在返回对象,但在 Vue Devtool 中它返回的是空数组
  • 没有错误?在控制器中?
  • 我在 axios 传递多个参数时遇到了问题,可能是相关的。我已经停止使用它,因为那里的人对社区没有任何了解,例如:github.com/axios/axios/issues/362#issuecomment-401014489 两年的错误,没有人关心...
  • 哦,所以有错误......它在 axios 中。也许你的参数。

标签: php laravel vue.js


【解决方案1】:

错误在最后一个方括号:

&lt;option v-for="city in cities" :value="city.id"&gt;@{{ city.name }&lt;/option&gt;

再放一个..

&lt;option v-for="city in cities" :value="city.id"&gt;@{{ city.name }}&lt;/option&gt;

【讨论】:

  • 那个错误只是在帖子中,即使语法正确也没有任何改变
猜你喜欢
  • 1970-01-01
  • 2021-05-13
  • 2019-08-09
  • 2017-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多