【发布时间】:2017-12-18 15:32:48
【问题描述】:
我希望能够进行 ajax 调用并使用返回的结果来生成使用vue.js 的下拉选项。 我可以这样做:
<select v-model="selected">
<option v-for="option in options" v-bind:value="option.value">
{{ option.text }}
</option>
</select>
<span>Selected: {{ selected }}</span>
.js 文件
new Vue({
el: '...',
data: {
selected: 'A',
options: [
{ text: 'One', value: 'A' },
{ text: 'Two', value: 'B' },
{ text: 'Three', value: 'C' }
]
}
})
但我不想让我的选项硬编码,而是来自 ajax 调用。
Ajax 调用看起来像这样:
function pullEmployees(){
var eventOwnerId = $('#eventOwner').val();
var eventOwnerName = $('#eventOwner :selected').text();
$.ajax({
url: "employees.cfm",
data: {
eventOwnerId: eventOwnerId,
eventOwnerName: eventOwnerName,
method : "updateOwners"
},
success: function(results) {
// results will have a list of objects where each objects has two properties (ID and Value)
}
});
}
我是vue.js 的新手,如果有人能提供帮助,我将不胜感激。
【问题讨论】:
-
那么到目前为止有什么问题?目前我在您的代码中看到的唯一问题是
this在您的success回调中不会是 Vue。 -
我的问题是我不知道如何将ajax调用的结果附加到vue对象的options属性上。如果您在上面的示例中看到,我已经对选项进行了硬编码(文本:'One',值:'A',文本:'Two',值:'B' 等)
-
您说对象以 {id, value} 的形式出现。他们需要是{text, value}吗?
-
实际上这是一个错字,它们以 {ID, text} 的形式出现。顺便说一句,我必须以某种方式告诉 vue 实例哪个是我的 id,哪个是我的文本或“this.vue.data.options = results;”行会知道如何渲染下拉菜单本身吗?
-
pullEmployees 在哪里?是vue方法还是vue之外的函数?
results到底是什么?只是一个选项对象数组?
标签: vue.js vue.js javascript vue.js vue-component