vue focus input

<template>
  <div>
    <input ref="myInput" v-focus />
  </div>
</template>

<script>
export default {
  directives: {
    focus: {
      // When the bound element is inserted into the DOM...
      inserted(el) {
        // Focus the element
        el.focus()
      }
    }
  }
}
</script>

Explanation:

  1. In the template section, an <input> element is defined with a ref attribute set to "myInput" and a custom directive v-focus is applied to it.

  2. In the script section, a Vue component is defined. Inside the component, a directives property is used to register a custom directive named "focus".

  3. The "focus" directive has a hook method named "inserted". This hook is called when the bound element is inserted into the DOM.

  4. Inside the "inserted" hook, the passed element (el) is focused using the el.focus() method.

  5. As a result, when the component is mounted, the input element with the ref "myInput" will receive focus, ensuring that it is focused when the page loads.