Generate Years from 1900 to current year with VueJS
- Don't use a method in the loop, use a computed property instead.
- Don't use
v-if
in av-for
element. It's bad!
new Vue ({
el: '.container',
computed : {
years () {
const year = new Date().getFullYear()
return Array.from({length: year - 1900}, (value, index) => 1901 + index)
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<div class="container">
<select id="dob">
<option value="0">Year:</option>
<option v-for="year in years" :value="year">{{ year }}</option>
</select>
</div>