Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get vue-select value not working

I am trying to get the selected value from vue-select but have used all methods and searched for help but can this to work, I also having the alert triggered when the page loads

Vue.component('v-select', VueSelect.VueSelect)
new Vue({
  el: '#app',
  data: {
    options: [      
      {id: 1, label: 'foo'},
      {id: 3, label: 'bar'},
      {id: 2, label: 'baz'},
    ],
    selected: '',
  },
  methods: {
    runme: function() {
      alert(this.selected);
    }
  }
})
body {
  font-family: 'Source Sans Pro', 'Helvetica Neue', Arial, sans-serif;
}

h1 {
  font-size: 26px;
  font-weight: 600;
  color: #2c3e5099;
  text-rendering: optimizelegibility;
  -moz-osx-font-smoothing: grayscale;
  -moz-text-size-adjust: none;
}

#app {
  max-width: 30em;
  margin: 1em auto;
}
<script src="https://vuejs.org/js/vue.js"></script>
<script src="https://unpkg.com/[email protected]/dist/vue-select.js"></script>
<div id="app">
  <h1>Vue Select - Using v-model</h1>
  <v-select v-model="selected" :on-change="runme" :options="options"></v-select>  
</div>
like image 938
Johan H. Avatar asked Mar 12 '26 09:03

Johan H.


1 Answers

vue-select author here. The on-change callback will be deprecated in v2.5.0 and removed in v2.6.0. Here's the prop from the v2.4.0 source:

/**
 * An optional callback function that is called  
 * each time the selected value(s) change.
 *
 * @type {Function}
 * @param {Object || String} val
 */
onChange: {
  type: Function,
  default: function (val) {
    this.$emit('input', val)
  }
}

As Bob Dust explained, the key here is that onChange calls this.$emit('input',val), which is what Vue hooks into to provide the v-model syntax. If the event is not emitted, Vue is unaware of the change.

If you need v-model and also want to take an action anytime the value changes, listening for the @input event is the best option:

<v-select v-model="selected" @input="runme" :options="options"></v-select>
like image 93
sagalbot Avatar answered Mar 13 '26 21:03

sagalbot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!