我已将v-model放入v-select中,但它返回了整个对象
<div id="app">
<h1>Vue Select - Using v-model</h1>
<v-select v-model="selected" :options="options" value="id" label="labels">
</v-select>
{{selected}}
</div>
Vue.component('v-select', VueSelect.VueSelect)
new Vue({
el: '#app',
data: {
options: [
{id: 1, labels: 'foo'},
{id: 3, labels: 'bar'},
{id: 2, labels: 'baz'},
],
selected: '',
}
})
是否有一种方法只能获取所选对象的ID,而不是整个对象?我曾尝试将value =“ id”放进去,但仍然无法正常工作。
答案 0 :(得分:2)
您最好的选择是使用computed
属性,以便您可以操纵selected
返回所请求的值:
computed: {
selectedID: function () {
return this.selected.id;
}
}
在您的示例中使用Codepen
答案 1 :(得分:0)
您是说
<v-select v-model="selected" :options="options" id=" {{selected.id}} " label="labels">
吗?
这会将选定的ID绑定到您的V型选择中。
答案 2 :(得分:0)
@Jiel,这是工作示例
Vue.component('v-select', VueSelect.VueSelect);
var app = new Vue({
el: '#app',
data: {
selected:'',
options: [
{ id: 0, labels: 'Vegetables' },
{ id: 1, labels: 'Cheese' },
{ id: 2, labels: 'Fruits' }
]
},
computed: {
selectedID: function () {
return this.selected.id
}
}
})
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://unpkg.com/vue-select@latest"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<div id="app">
<h1>Vue Select - Using v-model</h1>
<v-select v-model="selected" :options="options" label="labels">
</v-select>
selectedID : {{selectedID}}
</div>