Custom Directives

Learn about custom directives in Vue and practice writing them.

Directives let us attach encapsulated logic to otherwise unrelated components. Unlike mixins or even renderless components, they don’t imply any relationship between the components that use them. Directives are independent.

Vue offers many different directives, such as v-slot for using slots, and v-model for @input and :value, which is syntactic sugar. In addition, there are many more directives, such as v-if, v-for, and v-bind.

How to define custom directives

We can define custom directives via Vue.directive(). The method takes two arguments—the name of the directive and an object of options or a function. The name of the directive defines how we can use the directive. For example, if we give our ...