// Register a global custom directive called `v-focus`
Vue.directive('focus', {
// When the bound element is inserted into the DOM...
inserted: function (el) {
// Focus the element
el.focus()
}
})
Then in a template, you can use the new v-focus attribute on any element, like this:
<input v-focus>
Modular v-focus
// Register a global custom directive called `v-focus`
Vue.directive('focus', require('./lib/directives/v-focus'))
Then in a template, you can use the new v-focus attribute on any element, like this:
<input v-focus>
Auto import directives
Nuxt config and implementation
How to add more directives
Store your files in /lib/directives. Prepend your file with v- so your file look like this: v-directive-name.js.
Example
For example v-attach-my-animation.js. Directive name would be: attach-my-animation, then your template would be:
<div v-attach-my-animation>
</div>
Always kebab-case your JavaScript files for Vue directives, a reminder that HTML is case insensitive!