0

我正在使用 vue-select。我选择province_id然后创建记录成功,但是当显示记录时,我无法触发选择到select2的数据。

<v-select
  v-model="form.province_id"
  name="province_id"
  :options="provinces"
  :reduce="province => province.province_id" 
  label="name"
  :clearable="false"
  :searchable="true" />
import vSelect from 'vue-select'

export default {
  components: { 'v-select': vSelect },
  data () {
    const form = {
      province_id: ''
    }
    return {
      provinces: []
    }
  },
}```
4

1 回答 1

0

您的data声明错误,您需要form在返回的对象内部返回。

import vSelect from 'vue-select'

export default {
  components: { 'v-select': vSelect },
  data () {
    const 
    return {
      provinces: [],
      form: {
          province_id: undefined
      }
    }
  },
}

Vue.component('v-select', VueSelect.VueSelect);

new Vue({
  el: '#app',
  data() {
    return {
      provinces: [
        { name: 'province 1', province_id: 'p_1' },
        { name: 'province 2', province_id: 'p_2' },
        { name: 'province 3', province_id: 'p_3' },
      ],
      form: {
        province_id: ''
      }
    }
  },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<!-- use the latest vue-select release -->
<script src="https://unpkg.com/vue-select@latest"></script>
<link rel="stylesheet" href="https://unpkg.com/vue-select@latest/dist/vue-select.css">

<div id="app">
   Selected {{ form.province_id }}
  <v-select
    v-model="form.province_id"
    name="province_id"
    :options="provinces"
    :reduce="province => province.province_id" 
    label="name"
    :clearable="false"
    :searchable="true" />
</div>

于 2019-05-31T17:19:29.707 回答