Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
Vue - how to add custom directive with class define with `export default`

Looking at the documentation for custom directives: https://vuejs.org/v2/guide/custom-directive.html

The following example is given:

Vue.directive('pin', {
  bind: function (el, binding, vnode) {
    el.style.position = 'fixed'
    var s = (binding.arg == 'left' ? 'left' : 'top')
    el.style[s] = binding.value + 'px'
  }
})

new Vue({
  el: '#dynamicexample',
  data: function () {
    return {
      direction: 'left'
    }
  }
})

However, my class is structured with different syntax:

export default {
    name: 'dynamicexample',

In other words, I'm not using the new Vue keyword that is provided in the example.

Where and how do I add the directive in my code so it works?

The Vue.directive doesn't work if I just paste it above export default { name: 'dynamicexample',. That just causes the app to fail without any warning or error.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

new Vue({
  el: '#demo',
  directives: {
    focus: {
      inserted: function (el) {
        el.focus()
      }
    }
  }
})

Vue.config.productionTip = false
Vue.config.devtools = false
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <input type="text" v-focus />
</div>

You can try to create js file:

export const MyDirective {
  name: 'dynamicexample',
    /* your code */
}

then, in template where you use it :

import MyDirective from './directives/MyDirective.js';

export default {
    directives: {
       MyDirective
    }
    /* ... */
}

You can also use

Vue.directive('my-directive', MyDirective)

to declare it globally.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!