Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register a custom directive in Vue

Tags:

vue.js

In VueJs Docs, https://vuejs.org/guide/custom-directive.html I can easily create a custom directive using the native Vue.directive method

My question is how would you be able to create a custom directive inside an export default {}

the same as when registering a component or prop:

components: {
   Component1, Component2
},
methods: {
   method1() {
    // code here
   }
},
like image 983
raffffffff Avatar asked Jun 14 '16 03:06

raffffffff


People also ask

When to use custom directives in Vue?

In Vue 2.0, the primary code reuse and abstraction is components- however there may be cases where we need some low-level DOM access on plain elements, custom directives will still be useful here. A typical example would be focusing on an input element, like an input text.

How to manipulate Dom elements directly in Vue?

But while component options are useful for abstraction and code reuse, custom directives are still one of the best ways to directly manipulate DOM elements. Like component and its lifecycle hooks, each Vue directive has its own hooks that trigger

How to define a V-test directive in Vue?

There are two ways to define a custom directive in Vue. The first is the standard, which makes the directive available globally in the app: You can use this v-test directive anywhere in the app, but it doesn’t do anything right now, so we will add some functionality to it.

What is the use of V-model in Vue?

Vue also allows us to register our own custom directives aside the default set of directives that are shipped in core (v-show and v-model). In Vue 2.0, the primary code reuse and abstraction is components- however there may be cases where we need some low-level DOM access on plain elements, custom directives will still be useful here.


2 Answers

Here is a basical example about how to use directive in a component (*.vue file):

component/snippet.vue

<template>

    <div id="snippet">
        <code v-snippet>
            {{snippet.code}}
        </code>
    </div>

</template>


<script>

import snippet from '../directive/snippet';


export default {
    directives: {
        snippet: snippet
    }
}

</script>

directive/snippet.js

export default {

    update: function (el) {

        console.log('update');
    }
}

For further information, You can check https://v2.vuejs.org/v2/guide/custom-directive.html (the Intro part).

like image 175
胡亚雄 Avatar answered Sep 22 '22 13:09

胡亚雄


You can create a custom directive in this way

export default {
  components: {
    Component1, Component2
  },
  methods: {
    method1() {
      // code here
    }
  },
  directives: {
    // create a directive named 'elementLog'
    elementLog: {
      inserted: function (el) {
        console.log(el)
      },
    },
  },
}

In the template write the name of the directive in kebab-case format and add 'v-'

<template>
  <div>
    <span v-element-log>One</span>
    <span v-element-log>Two</span>
  </div>
</template>
like image 38
grovskiy Avatar answered Sep 20 '22 13:09

grovskiy