How it works…

The syntax to declare a new directive is as illustrated:

Vue.directive(<name: String>, {  // hooks})

This will register a new global directive. Inside the hooks object, you can define two important functions: bind, which you used in this recipe, and update, which is triggered every time a component contained in it is updated.

Every hook function is called with at least three arguments:

  • el: The HTML element
  • binding: Directives can receive an argument; binding is an object that will contain the value of the argument
  • vnode: the Vue internal representation of this element

We used the el parameter to edit the appearance of our element, manipulating it directly. 

Get Vue.js 2 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.