0
0
Vueframework~5 mins

Creating custom directives in Vue - Quick Revision & Summary

Choose your learning style9 modes available
Recall & Review
beginner
What is a custom directive in Vue?
A custom directive in Vue is a way to add reusable behavior to DOM elements, like changing styles or handling events, beyond what components do.
Click to reveal answer
beginner
How do you register a global custom directive in Vue 3?
Use app.directive('name', { mounted(el, binding) { /* code */ } }) where app is your Vue application instance.
Click to reveal answer
intermediate
What are the lifecycle hooks available in Vue 3 custom directives?
The main hooks are <code>created</code>, <code>beforeMount</code>, <code>mounted</code>, <code>beforeUpdate</code>, <code>updated</code>, <code>beforeUnmount</code>, and <code>unmounted</code>. They let you run code at different stages of the directive's life.
Click to reveal answer
intermediate
What does the binding argument provide in a directive hook?
It gives info about the directive usage like value (passed value), oldValue, arg (argument), and modifiers (special flags).
Click to reveal answer
beginner
Why use custom directives instead of components for DOM manipulation?
Custom directives are better for low-level DOM tasks like focusing an input or changing styles, where components would be too heavy or complex.
Click to reveal answer
Which Vue 3 directive hook runs when the element is inserted into the DOM?
Aupdated
Bmounted
Ccreated
DbeforeUnmount
How do you access the value passed to a custom directive in its hook?
Athis.value
Bel.value
Cbinding.value
Ddirective.value
Which of these is NOT a valid lifecycle hook for Vue 3 custom directives?
AbeforeDestroy
Bmounted
CbeforeMount
Dunmounted
What is the purpose of the modifiers property in the binding object?
ATo access the parent component's data
BTo store the element's CSS classes
CTo hold the directive's name
DTo pass extra flags to customize directive behavior
Where do you register a local custom directive in a Vue component?
AInside the <code>directives</code> option of the component
BIn the <code>methods</code> section
CIn the <code>data</code> option
DIn the <code>template</code> only
Explain how to create and use a simple custom directive in Vue 3 that changes the background color of an element.
Think about how you tell Vue to do something special when the element appears.
You got /4 concepts.
    Describe the role of the binding object in Vue custom directives and what information it provides.
    It's like a package of info about how the directive is used.
    You got /4 concepts.