【发布时间】:2019-07-31 19:39:27
【问题描述】:
我学习了 Vue JS,我有一个问题。我尝试在打开页面(我使用 Promise)之前使用后端查询初始化一个数组(listProductType)以与 ComboBox 一起使用。变量已初始化并在控制台显示,但 ComboBox 为空。
请帮助解决问题并修复代码。
HTML:
<div id="sendForm">
<div class="productTypeBlock">
<p><b>Product type</b></p>
<input id="idProductType" v-model="nameOfProductType" placeholder="Product type">
<button id="sendProductType" @click="getAlert">Сохранить</button>
</div>
<div class="productCategoryBlock">
<p><b>Product Category</b></p>
<select>
<option selected="selected" disabled>Product Category</option>
<option v-for='index in listProductType'>{{index.id}} / {{index.name}}</option>
</select>
</div>
</div>
main.js
var vm = new Vue({
el: "#sendForm",
data:{
nameOfProductType: "",
listProductType: []
},
beforeCreate:() => {
new Promise((resolve, _) => {
axios
.get('http://localhost/admin/getListProductType')
.then(response => {
resolve(this.listProductType = response.data);
console.log(listProductType);
});
})
},
methods:{
getAlert(){
var productType = {
name: this.nameOfProductType
};
//console.log(productType)
axios({
method: 'post',
url: 'http://localhost/admin/addProductType',
data: productType
});
}
}
});
【问题讨论】:
标签: javascript rest vue.js axios