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/vue-select@2.4.0/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>
vue-select
author here. Theon-change
callback will be deprecated inv2.5.0
and removed inv2.6.0
. Here's the prop from thev2.4.0
source:As Bob Dust explained, the key here is that
onChange
callsthis.$emit('input',val)
, which is what Vue hooks into to provide thev-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:Couple things here, first issue is with the
runme
method. By usingfunction
you are changing the context ofthis
. To access the data property you should be using es6 arrow syntax:Second, you really don't even need
selected
though, instead just pass the value as a parameter torunme
.Here is a working fiddle.
Updated your snippet
It was your handler on
change
event that suppresses the emitting ofinput
event (which actually plays in the 2 ways binding ofv-model
). You just need to listen toinput
instead: