// 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!
Subscribe to Harianto van Insulinde
Get the latest posts delivered right to your inbox
You've successfully subscribed to Harianto van Insulinde!
Subscribe to Harianto van Insulinde
Stay up to date! Get all the latest & greatest posts delivered straight to your inbox