<input type="text" v-model="name" ref="getFocus" /> <script> export default { data() { return { name: ‘‘ } }, mounted() { this.$refs.getFocus.focus() } } </script>
Vue.directive(‘getFocus‘, { inserted: function(el, binding) { el.focus() } }) <input type="text" v-model="name" v-getFocus />
<input type="text" v-model="name" id="getFocus" /> <script> export default { data() { return { name: ‘‘ } }, mounted() { document.getElementById(‘getFocus‘).focus() } } </script>
原文:https://www.cnblogs.com/wu-hen/p/13931492.html