我正在从数据库中获取值到下拉框所在的页面,但下拉框未返回该值。代码如下所示,
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);
});
}
}
<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>
答案 0 :(得分:1)
我发现有两件事是错误的...
Countries
在form
对象内部,但是您不从那里分配或读取它。将其移到顶层
您正在将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