admin管理员组

文章数量:1429053

I have directive and I wanted to create a local method inside the directive and use it in the hook function. Here's my code:

export const OutsideClick = {
  bind (el, binding, vnode) {
    console.log(new Vue());
    // call method1()
  },
  ponentUpdated(el, binding, vnode) {
    console.log('updated p', binding);
    if(binding.value[1]()) {
      // call method1();
    }
  },
  unbind(el, binding, vnode) { 
    console.log('unbinding');
  }
}

So at this point how to define the function inside the directive and use it inside the bind and ponentUpdated ?

I have directive and I wanted to create a local method inside the directive and use it in the hook function. Here's my code:

export const OutsideClick = {
  bind (el, binding, vnode) {
    console.log(new Vue());
    // call method1()
  },
  ponentUpdated(el, binding, vnode) {
    console.log('updated p', binding);
    if(binding.value[1]()) {
      // call method1();
    }
  },
  unbind(el, binding, vnode) { 
    console.log('unbinding');
  }
}

So at this point how to define the function inside the directive and use it inside the bind and ponentUpdated ?

Share Improve this question edited Mar 14, 2019 at 8:33 varun agarwal 1,5098 silver badges10 bronze badges asked Mar 14, 2019 at 7:36 Víñịt VịłłăVíñịt Vịłłă 2332 silver badges10 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 2

I don't think you can add a method inside the directive itself. But you can declare the method outside the directive and call it from inside it.

function method1 (el, binding, vnode) {
 ...
}

export const OutsideClick = {
 bind (el, binding, vnode) {
  console.log(new Vue());
  method1(el, binding, vnode)
 },
 ponentUpdated(el, binding, vnode) 
 {
  console.log('updated p', binding);
  if(binding.value[1]()) {
   method1(el, binding, vnode)
  }
 },
 unbind(el, binding, vnode) { 
  console.log('unbinding')
  method1(el, binding, vnode)
 }
}

Well, you need to add the function outside of the directive and call it inside the lifecycle methods as per below example.

Vue.directive("color", {

  "bind": function() {
    console.log("directive active");
    hello();
  },


  "unbind": function() {
    console.log("directive deactive");
  },


  "update": function(newValue, oldValue) {
    var el = $(this.el); 


    if (this.arg == 'background')
      el.css('background', newValue);
    else
      el.css('color', newValue);
  },
});

function hello() {
  console.log('hello');
}

new Vue({
  el: '#app'
});
<script src="https://cdnjs.cloudflare./ajax/libs/vue/1.0.24/vue.min.js"></script>
<script src="https://code.jquery./jquery-2.2.4.min.js"></script>

<div id="app">
  <h2>Color</h2>
  <select v-model="color">
    <option value="#f00">Red</option>
    <option value="#0f0">Green</option>
    <option value="#00f">Blue</option>
    <option value="#000" selected>Black</option>
  </select>
  <br><br>
  <div v-color="color">
    Hello world!!!
  </div>
  <h2>Background</h2>
  <select v-model="color2">
    <option value="#f00">Red</option>
    <option value="#0f0">Green</option>
    <option value="#00f">Blue</option>
    <option value="#000" selected>Black</option>
  </select>
  <br><br>
  <div v-color:background="color2">
    Hello world!!!
  </div>

</div>

Maybe someone will find it helpful. The trick is to wrap a directive with a constructor function.

function myDirective() {
  this.myMethod = function() {
    console.log('My method')
  }
  return {
    bind: function(el) {
      el.addEventListener('click', this.myMethod)
    }.bind(this),
    update: function() {
      this.myMethod()
    }.bind(this),
    unbind: function(el) {
      el.removeEventListener('click', this.method)
    }.bind()
  }
}

Vue.directive('myDirective', new myDirective())
new Vue({
  el: '#app'
});
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <button v-my-directive>Just a button</button>
</div>

Also functions with .bind(this) could be replaced with lambdas () => {}.

本文标签: javascripthow to define a method inside the Vue directiveStack Overflow