值未进入Vue JS的下拉框中

时间:2018-11-14 05:16:36

标签: php laravel vue.js

我正在从数据库中获取值到下拉框所在的页面,但下拉框未返回该值。代码如下所示,

脚本

methods: {
        editState(id){
            axios.defaults.headers.common['Authorization'] = "Bearer "+localStorage.getItem('token'); 
              axios.get(baseUrl+'/state/edit/'+id)
              .then((response) => {

                    alert(response.data[0].form.country_name);

                    this.form = response.data[0].form;

                     setTimeout(() => {
                         this.subComponentLoading = true;
                    }, 500);
                })
                .catch(function (error) {
                console.log(error);
            });
        }
       }

Vue

<d-field-group class="field-group field-row" label-for = "country_name" label="Country Name" label-class="col-4">
    <d-select :options="Countries" v-model="form.country_id" id="country_id" name = "country_name"  wrapper-class="col-7">

    </d-select>
</d-field-group>

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

我发现有两件事是错误的...

  1. Countriesform对象内部,但是您不从那里分配或读取它。将其移到顶层

  2. 您正在将v-model绑定到form.country_id,但是最初并不存在。将其添加到form对象。

总结一下...

data () {
  return {
    isStateEditVisible: false, 
    form: {
      state_name: '', 
      isStateEnabled: true, 
      ISO_Code: '', 
      country_id: '', //  added this
      country_name: '', 
      zone_name: ''
    },
    Countries: [],    //  moved this
    Zones: []         //  and this
  }
}

为了对数据更改做出反应,Vue需要预先了解它们。参见https://vuejs.org/v2/guide/reactivity.html