import { defineDirective } from 'vue';
export const vFocus = defineDirective({
// called before bound element's attributes or event listeners are applied
created(el, binding, vnode, prevVnode) {
// do something on element creation
},
// called when the bound element’s parent component is mounted
mounted(el, binding, vnode, prevVnode) {
el.focus();
},
// called before the parent component is updated
beforeUpdate(el, binding, vnode, prevVnode) {
// do something before update
},
// called after the parent component and its children are updated
updated(el, binding, vnode, prevVnode) {
// do something after update
},
// called when the bound element’s parent component is unmounted
beforeUnmount(el, binding, vnode, prevVnode) {
// do something before unmount
},
// called when the bound element’s parent component is unmounted
unmounted(el, binding, vnode, prevVnode) {
// do something when unmounted
}
});
<template>
<input v-focus>
</template>
<script setup>
// Можно пустым оставить, так как мы просто показываем пример использования директивы
</script>
<style>
/* Стили могут быть добавлены по желанию */
</style>
Это простая директива, которая фокусируется на элементе ввода, когда он монтируется. Вы можете расширять это по мере необходимости, добавляя дополнительные функциональные возможности в различных хуках жизненного цикла директивы.