简体   繁体   English

Vue选择如何将1个属性绑定到v模型

[英]Vue Select how to bind 1 property to v-model

I've put v-model in v-select but it returns the whole object 我将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: '',
  }
})

this will result to this 这将导致 在此处输入图片说明

is there a way to get the selected objects id only instead of the whole object? 有没有一种方法来获取所选对象的ID,而不是整个对象? I've tried putting value="id" but still doesn't work. 我曾尝试将value =“ id”放进去,但仍然无法正常工作。

Your best option would be to use a computed property so you can manipulate selected to return your requested value: 最好的选择是使用computed属性,以便您可以操纵selected返回所需的值:

computed: {
    selectedID: function () {
      return this.selected.id;
    }
  }

Working Codepen with your example 在示例中使用Codepen

Do you mean 你的意思是

<v-select v-model="selected" :options="options" id=" {{selected.id}} " label="labels"> ? <v-select v-model="selected" :options="options" id=" {{selected.id}} " label="labels">吗?

This will bind the selected ID into your V-select. 这会将选定的ID绑定到您的V型选择器中。

@Jiel , here is the working demo @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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM