Search code examples
vue.jsvuetify.jsv-select

How to pass value from child to parent for v-select component


I am sure I am just overlooking the obvious, but I am having a little trouble with this one: simple component with one v-select control and passing the selected value to the parent. My code:

//CHILD CONTROL

    <template>
    <v-container fluid grid-list-md>
        <v-layout row wrap>
            <v-flex d-flex xs12 sm6 md8>
                <v-flex xs12 sm6 offset-sm3>
                    <v-flex xs12 sm6 d-flex>
                        <v-select :placeholder="placeholder"
                                  :items="options"
                                  item-value="id"
                                  item-text="name"
                                  class="form-control-select"
                                  @input="changeMonth()"
                                  v-model="selectedOption">
                        </v-select>
                    </v-flex>
                    <span>Child component: {{ selectedOption }}</span>
                </v-flex>
            </v-flex>
        </v-layout>
    </v-container>
</template>

<script>
    export default {
        name: 'FormSelect',
        props: {
            placeholder: { type: String },
            options: { type: Object }
        },
        data: function () {
            return {
                selectedOption: 0
            }
        },
        methods: {
            changeMonth: function () {
                console.log("child changed: " + this.selectedOption);
                this.$emit('change', this.selectedOption);
            }
        }
    }
</script>

//PARENT

    <template>
    <div>
        <div>
            <dropdown2 id="component-dropdown2" :options="months" :placeholder="placeholderValue" v-model="selectedMonth" @change="monthChanged"></dropdown2>
            <span>Parent component: {{ selectedMonth}}</span>
        </div>
    </div>
</template>

<script>
    import Dropdown2 from '../addons/Dropdown2';

    export default {
        components: {
            Dropdown2,

        },
        data() {
            return {
                placeholderValue: 'Month',
                months: [{ id: 1, name: 'Jan' }, { id: 2, name: 'Feb' }, { id: 3, name: 'Mar' },],
                selectedMonth: null
            }
        },
        methods: {
            monthChanged() {
                console.log("Month changed. Selected ID: ", this.selectedMonth);
            }
        }
    }
</script>

I have no idea what else to try anymore; can anybody perhaps assist me with this?

Many thanks in advance!

N.


Solution

  • The convention is to accept the value as a bound property called value, and emit the desired new value as an event called input.

    v-model expects the event to be called input, and not change.