Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue.js v-select default value

I want to make a default value of the select option using vue.js

here is my code

 <v-select
        v-model="contact.title"
        :options="['Mr','Mrs','Ms']">
      </v-select>

and

export default {
 props: {
  contact: {
  type: Object,
  required: true,
 },

titles: {
     type: Array,
     required: true,
   },
  },
};

thanks

like image 407
Tessa Muliawati Avatar asked Nov 19 '25 08:11

Tessa Muliawati


2 Answers

Try this.I think this will work.

<v-select
  v-model="selected"
  :options="options">
</v-select>


data: () {
  return {
    selected: 'Option 1',
    options: ["Option 1","Option 2","Option 3"]
  }
},
like image 174
Bhaskararao Gummidi Avatar answered Nov 21 '25 07:11

Bhaskararao Gummidi


Mutating a prop is not best practice in vue. You could do it like:

<v-select
    v-model="selected"
    :options="['Mr','Mrs','Ms']">
</v-select>


data: function () {
  return {
    selected: '' || 'defaultValue'
  }
},

This way you are not mutating the prop and you easily can set a default value.
If you want to pass the data to the parent look at:
Pass data to parent

like image 25
mava Avatar answered Nov 21 '25 08:11

mava