0
0
Vueframework~10 mins

Why custom directives matter in Vue - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why custom directives matter
Vue Component Renders
Template with Directive
Vue Compiler Detects Directive
Custom Directive Hook Called
Directive Logic Executes
DOM Element Updated
User Sees Effect
Vue renders a component, finds a custom directive in the template, runs its logic, and updates the DOM element accordingly.
Execution Sample
Vue
<template>
  <input v-focus />
</template>
<script>
export default {
  directives: {
    focus: {
      mounted(el) { el.focus() }
    }
  }
}
</script>
This code uses a custom directive 'v-focus' to automatically focus the input element when it appears.
Execution Table
StepActionDirective HookElement StateEffect
1Vue renders componentNo directive hook yetInput element createdNo focus yet
2Vue compiler finds v-focus directivePrepare to call mounted hookInput element readyNo focus yet
3Call mounted(el) hookmounted calledInput element focusedCursor blinks inside input
4User sees input focusedDirective logic doneInput element focusedUser can type immediately
💡 Directive mounted hook ran once, input element is focused, no further action needed
Variable Tracker
VariableStartAfter 1After 2Final
el.focusedfalsefalsefalsetrue
Key Moments - 2 Insights
Why does the input get focused automatically?
Because the custom directive's mounted hook runs after the element is created, calling el.focus() as shown in execution_table step 3.
Can we use custom directives for things other than focus?
Yes, custom directives can run any DOM-related logic when elements mount, update, or unmount, making them very flexible.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the input element become focused?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Check the 'Element State' and 'Directive Hook' columns in step 3.
According to variable_tracker, what is the value of el.focused after step 2?
Atrue
Bfalse
Cundefined
Dnull
💡 Hint
Look at the 'After 2' column for el.focused in variable_tracker.
If the mounted hook did not call el.focus(), what would change in the execution table?
AInput element would still be focused at step 3
BDirective hook would not be called at all
CInput element would not be focused after step 3
DUser would see input focused immediately at step 1
💡 Hint
Refer to the 'Effect' column in execution_table step 3 and 4.
Concept Snapshot
Custom directives in Vue let you add special behavior to DOM elements.
They run hooks like mounted when elements appear.
Use them to manipulate elements directly, like focusing inputs.
They keep your templates clean and reusable.
Directive hooks run after element creation, updating the DOM.
This helps add interactive features easily.
Full Transcript
When Vue renders a component, it looks for any custom directives in the template. For example, if an input has a v-focus directive, Vue detects it during compilation. Then Vue calls the directive's mounted hook once the input element is created in the DOM. Inside this hook, the directive runs code like el.focus() to focus the input automatically. This updates the element's state so the user sees the cursor blinking inside the input, ready to type. Custom directives matter because they let you add reusable, direct DOM behavior without cluttering your component logic. They run at specific lifecycle moments, like when elements mount, update, or unmount, giving you control over element behavior. This visual trace shows how the directive hook runs after rendering and changes the element state, making the input focused for the user.