3

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

4
  • Just set a default value for the title field of the contact object passed as prop. Commented Jan 10, 2019 at 17:13
  • You have to set the the value of contract.title to one of the values from the array ['Mr','Mrs','Ms'] Commented Jan 10, 2019 at 17:14
  • this will fire an error, since props don't support two ways binding Commented Jan 10, 2019 at 17:16
  • @mava answer looks good, I completly missed that it's props Commented Jan 10, 2019 at 17:42

2 Answers 2

4

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"]
  }
},
Sign up to request clarification or add additional context in comments.

Comments

3

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

2 Comments

What is not working? The default value? Do you have any errors in the console?
yes the default value can't display, nothing errors in the console

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.