Custom directives تسمح لك بـ تغليف معالجة DOM منخفضة المستوى كـ v-* attribute قابل لإعادة الاستخدام. استخدمها عندما تحتاج إلى الوصول المباشر إلى عنصر (التركيز، التمرير، مكتبات DOM للجهات الخارجية) — أشياء لا تغطيها المكونات والـ props بشكل طبيعي.
تعريف directive محلي
<script setup>
// a directive is an object of lifecycle hooks; in <script setup>, name it vXxx
const vFocus = {
mounted(el) {
el.focus(); // `el` is the raw DOM element
},
};
</script>
<template>
<input v-focus /> <!-- autofocuses on mount -->
</template>
