0
0
Vueframework~10 mins

Creating custom directives in Vue - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating custom directives
Define directive object
Register directive globally or locally
Use directive in template
Vue calls directive hooks
created
Update hooks if reactive data changes
Directive cleanup on unmount
You define a directive, register it, then Vue calls its hooks as the element is created, updated, and removed.
Execution Sample
Vue
const focusDirective = {
  mounted(el) {
    el.focus()
  }
}
app.directive('focus', focusDirective)
This code creates a custom directive that focuses an input element when it is mounted.
Execution Table
StepActionDirective Hook CalledElement StateEffect
1Directive object definedNoneNo element yetNo effect
2Directive registered globallyNoneNo element yetNo effect
3Template uses v-focus on inputNone<input>No effect yet
4Vue creates input elementcreated<input>Element created but not in DOM
5Input element about to be inserted in DOMbeforeMount<input>Preparing to mount
6Input element fully mountedmounted<input>Input receives focus
7Reactive data changes (if any)updated<input>Directive can react to changes
8Component unmountsunmounted<input>Cleanup if needed
9Directive lifecycle endsNoneElement removedFocus lost, element gone
💡 Directive lifecycle ends when element is removed from DOM and component unmounts.
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 8Final
el (element)undefined<input><input focused><input removed>undefined
directive hooksnot callednot calledmounted calledunmounted calledcompleted
Key Moments - 3 Insights
Why does the focus happen only at the mounted hook and not earlier?
Because the element is only fully inserted into the DOM at the mounted hook, so calling focus before that won't work. See execution_table step 6.
Can the directive respond to reactive data changes?
Yes, the updated hook runs when reactive data changes affect the element. See execution_table step 7.
What happens if the directive is not cleaned up on unmount?
It can cause memory leaks or unexpected behavior. The unmounted hook is for cleanup. See execution_table step 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the input element receive focus?
AStep 3
BStep 4
CStep 6
DStep 8
💡 Hint
Check the 'Effect' column for when the input receives focus.
According to variable_tracker, what is the state of 'el' after step 8?
A<input removed>
B<input>
C<input focused>
Dundefined
💡 Hint
Look at the 'el (element)' row under 'After Step 8' column.
If the directive did not implement the unmounted hook, what might happen?
AThe element would never mount.
BMemory leaks or leftover effects might occur.
CFocus would never be applied.
DThe directive would not be registered.
💡 Hint
Refer to key_moments about cleanup on unmount.
Concept Snapshot
Creating custom directives in Vue:
- Define a directive object with hooks (created, beforeMount, mounted, updated, unmounted).
- Register it globally or locally.
- Use it in templates with v-yourDirective.
- Vue calls hooks as element lifecycle progresses.
- Use mounted to access DOM, unmounted for cleanup.
Full Transcript
Creating custom directives in Vue involves defining an object with lifecycle hooks like mounted and unmounted. You register this directive globally or locally in your app. When you use the directive in your template, Vue calls these hooks as the element is created, inserted into the DOM, updated, and eventually removed. For example, the mounted hook is where you can safely manipulate the DOM element, such as setting focus. The unmounted hook is important for cleaning up any side effects to avoid memory leaks. This flow ensures your directive behaves correctly with the element's lifecycle.