Below is my code. I want to use "select" tag and I want to render the options and its value from the script. I have used "v-bind:value" for rendering the values from the script. But there is an error such as ***"
[eslint-plugin-vue] [vue/require-v-for-key] Elements in iteration expect to have 'v-bind:key'
directives."***
Hence I have given like this **"
< option v-for= "option in nationalityOpt" v-bind:value="option.value" v-bind:key = ""> {{ option.text }} < / option>
".
If I removed the "v-bind:value" from my code then I could not get the option values in the UI. I want to rectify the error for "v-bind:key" and I would like to know the difference between those two. Please help.
<template>
<b-card>
<h4 slot="header" class="card-title">Employee</h4>
<b-row>
<b-col sm="3">
<b-form-group>
<label for="name">First Name </label>
<input type="text" id="name" class="form-control" placeholder="Enter your name" v-model="firstName">
</b-form-group>
</b-col>
</b-row>
<b-row>
<b-col sm="3">
<b-form-group>
<label for="name">Nationality</label>
<select name="" id="" class="form-control" placeholder="Nationality" v-model="nationality">
<option v-for="option in nationalityOpt" v-bind:value="option.value"> {{ option.text }} </option>
</select>
</b-form-group>
</b-col>
</b-row>
<input type="submit" value="Submit" @click="validateForm">
</b-card>
</template>
<script>
export default {
name: 'addEmpl',
data () {
return {
firstName: '',
nationality: '1',
nationalityOpt: [
{ value: '1', text: 'Select' },
{ value: 'IN', text: 'Indian'},
{ value: 'OT', text: 'Others'}
],
}
}
}
</script>
Thanks in advance.
Check the docs for List Rendering, specifically about the keys.
You need the v-bind:key
in a v-for because it needs to differentiate each component rendered, in case of data changing. You need to use the both, v-bind:key
and v-bind:value
in your component option
, to make it work as you expect.
You can simplify and use just :key
and :value
, like:
<option v-for="option in nationalityOpt" :value="option.value" :key="option.value">
{{ option.text }}
</option>
Vue.js has some methods to increase rendering efficiency by reusing component. To make the list (with v-for) render correctly, we should provide unique :key binding to each element https://v2.vuejs.org/v2/guide/list.html#key
<select name="" id="" class="form-control" placeholder="Nationality" v-model="nationality">
<option v-for="option in nationalityOpt" v-bind:value="option.value" :key="option.value"> {{ option.text }} </option>
</select>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With