0

As a newbie to Vue.js, I'm trying to render this array:

       countries: ["US", "UK", "EU" ]

Into a select menu:

<select>
  <option disabled value="">Your Country</option>
  <option v-for="(index, c) in countries" :key="index">{{ c }}</option>
</select>

And put UK as default selected item. But instead of Country codes, just cuntries being shown in the menu.

How can I fix this?

3
  • You say "store" are you using Vuex then? Commented Sep 14, 2018 at 11:01
  • Yes, but never mind, that's another story that I've resolved. Commented Sep 14, 2018 at 11:02
  • 1
    Ok good luck on the rest of your adventure! Commented Sep 14, 2018 at 11:02

2 Answers 2

1

To do what you want and set "UK" as a default add a 'v-model' to your select which you probably want anyway

<select v-model="selected">
  <option v-for="(c,index) in countries" :key="index" >{{ c }}</option>
</select>

Then in your data object set selected to your default value.

data(){
  return{
     countries: ["US", "UK", "EU" ],
     selected: "UK",
  }
}

Modified working example here -> https://jsfiddle.net/skribe/0wmnkfpz/6/

Sign up to request clarification or add additional context in comments.

1 Comment

Please modify your answer above rather than putting it in comments.
1

I believe you have the order of index and c flipped. It should be:

<option v-for="(c, index) in countries" :key="index">{{ c }}</option>

See examples on https://v2.vuejs.org/v2/guide/list.html

2 Comments

@Babr then you have something else wrong. KJ789's answer should work. See here jsfiddle.net/skribe/0wmnkfpz/2
Alright, perhapas there is some typo in my original array. In any case How can I put UK as defaut here?

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.