How to do it...

We will build a v-pony directive that will turn any element into a pony element. A pony element is created with a pink background and changes color when you click on it.

The HTML code for the pony element is as follows:

<div id="app">  <p v-pony>I'm a pony paragraph!</p>  <code v-pony>Pony code</code>  <blockquote>Normal quote</blockquote>  <blockquote v-pony>I'm a pony quote</blockquote></div>

Just to show the difference, I've included a normal blockquote element. In our JavaScript section, write the following:

Vue.directive('pony', {  bind (el) {    el.style.backgroundColor = 'hotpink'  }})

This is how you declare a new directive. The bind hook is called when the directive is bound to the element. The only thing we are doing ...

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.