I am trying to remember how to return the value within the select from a component.
Despite I bet is pretty easy I forgot or I am just doing it wrong.
I call the component:
<custom-select :options="options" v-model="selectedMain"></custom-select>
And then I have the component itself:
<template>
<fieldset class="form-group">
<select v-model="selected"
id="something"
name="something">
<option :value="null">Select an option</option>
<option v-for="option in options"
value="option.id"
v-html="option.name"></option>
</select>
</fieldset>
</template>
<script>
module.exports = {
props: [ 'options' ],
data: function() {
return {
selected: null
}
}
}
</script>
This is defined in my main Vue instance of course and it renders ok:
new Vue({
...
data: function() {
return {
...
selectedMain: null
...
}
},
...
})
But when I change the value to 1 (for instance) from the dropdown I can see in the Vue Debugger toolbar that:
<Root> selectedMain: null <CustomSelect> selected: 1 </CustomSelect> </Root>
Edit 1: I think this is pretty the same thing:
Vue.component('custom-select', {
data () {
return {
selected: null
}
},
props: [ 'options' ],
template: `<fieldset class="form-group">
<select v-model="selected"
id="something"
name="something">
<option :value="null">Select an option</option>
<option v-for="option in options"
value="option.id"
v-html="option.name"></option>
</select>
</fieldset>`
});
new Vue({
el: '#app',
data: function() {
return {
options: [
{ id: 1, name: "First option" },
{ id: 2, name: "Second option" },
{ id: 3, name: "Third option" },
],
selectedMain: null
}
},
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div id="app">
<custom-select :options="options" v-model="selectedMain"></custom-select>
<p>Selected Main: {{ selectedMain }}</p>
</div>
Therefore I want obviously selectedMain takes the selected value.
How to achieve this? Any suggestions are appreciated.