search

Home  >  Q&A  >  body text

javascript - What is the difference between global methods, global resources and instance methods in vue.js plug-in?

http://cn.vuejs.org/v2/guide/...

MyPlugin.install = function (Vue, options) {
  // 1. 添加全局方法或属性
  Vue.myGlobalMethod = function () {
    // 逻辑...
  }
  // 2. 添加全局资源
  Vue.directive('my-directive', {
    bind (el, binding, vnode, oldVnode) {
      // 逻辑...
    }
    ...
  })
  // 3. 注入组件
  Vue.mixin({
    created: function () {
      // 逻辑...
    }
    ...
  })
  // 4. 添加实例方法
  Vue.prototype.$myMethod = function (options) {
    // 逻辑...
  }
}

What are the differences between 1, 2, and 4 here?

仅有的幸福仅有的幸福2789 days ago899

reply all(3)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-16 13:39:57

    Let me explain it briefly:

    1 Global method, which can be understood as window. myGlobalMethod 一样,通过 Vue.myGlobalMethod to call, is just a static method defined under Vue

    2 Global resources. In the example, a global directive is defined. For details, please refer to the custom directive chapter of vue. There is no difference. It just means that a directive is also defined in your plug-in. Of course, you can also define filters and other operations. It all depends on what you want to do with this plug-in

    4 Instance method, recall the concept of class in JS and the meaning of prototype prototype chain. If you don’t understand it, let’s take a look at these basic contents first.
    Here I can explain it to you like this, instance methods can be called inside the component through this.$myMethod

    reply
    0
  • 阿神

    阿神2017-05-16 13:39:57

    1. Global method = static method of class

    2. Global resources = global instructions, which are instructions similar to v-for, but customized

    3. Global instance method = instance method of class

    Second point, please look directly at the chapter of vue custom instructions. 1 and 3 are JavaScript content, please find the reference book by yourself.

    reply
    0
  • 迷茫

    迷茫2017-05-16 13:39:57

    Just take a look at the plug-in source code

    reply
    0
  • Cancelreply