1

I am trying to use the vue-select component for a dropdown list. So far I have written.

<template>
    <div>
        <v-select label="name" key="id" :v-model="selected" :reduce="data => data.id" :options="items" @input="update()" />
    </div>

</template>

<script>
export default {
  props: {
      initial: {
        type: [String, Number],
        default: 0,
      },
      api_call: {
          type: String,
          required: true,
      },

  },

  data(){

      return {
          value: this.initial,
          items: [],
      }

  },

  computed: {
      selected: {
          get() {
              return this.value;

          },
          set(val) {
              return this.value = val;
          }
      },
  },
  methods:{
      update() {
          console.log('selected', this.selected, this.value);
          this.$emit('input', this.selected);

      },
      getData: function(){

        axios.get('/api/' + this.api_call)
        .then(function (response) {
            this.items = response.data;
        }.bind(this));

      },
    },
    created(){
      this.getData();
    }

  }

The dropdown list populates as intended and selecting an Item inserts it in the input filed. The two problems I have are

  1. Neither the value nor the selected variables change when something is selected.
  2. I am also passing in an initial value which I would like to be selected as the default in the list.

1 Answer 1

1

Remove the binding sign : from v-model directive

 <v-select label="name" key="id"  v-model="selected" :reduce="data => data.id" :options="items" @input="update()" />

and init your value like :

  data(vm){//vm refers to this

      return {
          value: vm.initial,
          items: [],
      }

  },

or :

  data(){

      return {
          value: null,
          items: [],
      }

  },
mounted(){
this.value=this.initial
}
Sign up to request clarification or add additional context in comments.

1 Comment

just removing the : made everything work for me. That little mistaken had me banging my head against my keyboard for hours. Thank you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.