Vue · Middle · Question 25 of 75
How do you write a local custom directive in <script setup>?
<script setup>
const vFocus = { mounted: (el) => el.focus() };
</script>
<template><input v-focus /></template>- ✓Correct answer: Any camelCase variable starting with
vbecomes a directive usable asv-focus - 2Directives can only be registered globally
- 3With
defineDirective('focus')
Explanation
Directive hooks mirror lifecycle hooks (created, mounted, updated, unmounted). Use directives for low-level DOM access; prefer components for anything with its own markup.