0👍
Create yourself a little v-focus
directive and on inserted
use the el
argument passed to the directive to call focus()
on.
Vue.directive('focus', {
inserted (el) {
el.focus();
}
})
new Vue({
el: '#example',
data: {
inputs: ['', '', '']
},
methods: {
add () {
this.inputs.push('')
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.2.4/vue.js"></script>
<div id="example">
<input v-for="(input, i) in inputs" v-focus />
<button @click="add">add</button>
</div>
Source:stackexchange.com