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>

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.

Further reading

Problem with a question

Vue · Middle · question 25

Test yourself

Take the Vue · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz