3👍
Create a directives file called /src/directives/directives.js. I have placed my file into a directives folder to keep it clean.
// focus text input on mounted
const focus = {mounted: (el) => el.focus()}
export { focus }
In the main.js file import your directive and globally register it to your app.
import { createApp } from 'vue'
import { focus } from "./directives/directives.js"
const app = createApp(App)
app.directive("focus", focus)
.mount('#app')
Finally, in your component use the directive.
<input type="text" v-focus>
1👍
Simply rename the variable name to vFocus !
Here another example with Vue + Typescipt
create a directive file eg. ‘HelloDirective.ts’
export const vSayHello = { mounted: (el: Element) => (el.innerHTML = 'hello') }
import into your component with
import { vSayHello } from '@/directives/HelloDirective'
add directive into your html template
<span v-say-hello>old text</span>
- [Vuejs]-How to assign event handlers to custom elements
- [Vuejs]-Call laravel {{action(Controller@method}} with passing variable from vue.js array
Source:stackexchange.com