【问题标题】:how to define a method inside the Vue directive?如何在 Vue 指令中定义方法?
【发布时间】:2019-08-05 00:45:09
【问题描述】:

我有指令,我想在指令中创建一个本地方法并在钩子函数中使用它。这是我的代码:

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

那么此时如何在指令里面定义函数并在bindcomponentUpdated里面使用呢?

【问题讨论】:

    标签: javascript vue.js vuejs2 vue-component vue-directives


    【解决方案1】:

    我认为您不能在指令本身内添加方法。但是你可以在指令之外声明方法,然后在里面调用它。

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

    【讨论】:

    • 非常感谢:) ...我想把它放在指令中。我知道它可以在外面声明并在里面使用它。我的确切问题是将它放在指令中。但是 +1 票。
    【解决方案2】:

    好吧,您需要在指令之外添加函数并在生命周期方法中调用它,如下例所示。

    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.com/ajax/libs/vue/1.0.24/vue.min.js"></script>
    <script src="https://code.jquery.com/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>

    【讨论】:

      【解决方案3】:

      也许有人会觉得它有帮助。诀窍是用构造函数包装指令。

      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.com/ajax/libs/vue/2.5.17/vue.js"></script>
      <div id="app">
        <button v-my-directive>Just a button</button>
      </div>

      .bind(this) 的函数也可以替换为 lambdas () =&gt; {}

      【讨论】:

        猜你喜欢
        • 2021-11-14
        • 2018-10-20
        • 2019-11-06
        • 1970-01-01
        • 2013-05-28
        • 2019-11-19
        • 1970-01-01
        • 2020-06-16
        • 1970-01-01
        相关资源
        最近更新 更多