Complete the code to define a Vue directive with a mounted hook.
const myDirective = {
[1](el) {
el.style.color = 'blue'
}
}The mounted hook runs when the directive is attached to the element.
Complete the code to update the element's text color when the directive updates.
const myDirective = {
updated(el, binding) {
el.style.color = [1]
}
}binding.oldValue which is the previous value, not the current one.The binding.value holds the new value passed to the directive.
Fix the error in the directive by completing the unmounted hook to clean up.
const myDirective = {
unmounted(el) {
el.[1] = null
}
}style or textContent which is not event cleanup.Cleaning event handlers like onclick prevents memory leaks when the element is removed.
Fill both blanks to create a directive that changes background color on mount and cleans event on unmount.
const colorDirective = {
[1](el) {
el.style.backgroundColor = 'yellow'
},
[2](el) {
el.onclick = null
}
}updated instead of mounted for initial style.The mounted hook sets styles when the element appears, and unmounted cleans event handlers when removed.
Fill all three blanks to create a directive that sets text color on mount, updates it on update, and removes click handler on unmount.
const textColorDirective = {
[1](el) {
el.style.color = 'green'
},
[2](el, binding) {
el.style.color = [3]
},
unmounted(el) {
el.onclick = null
}
}created which is not a directive hook.binding.value to get the new color.mounted sets initial color, updated changes color when value changes, and binding.value holds the new color.