11

Vue.js official docs about plugins describes global methods and properties and Vue instance methods.

// 1. add global method or property
Vue.myGlobalMethod = function () {
  // some logic ...
}

// 4. add an instance method
Vue.prototype.$myMethod = function (methodOptions) {
  // some logic ...
}

But it isn't clear which of this approach is better fit to define global functionality? Can someone explain difference or indicate some resource about different use cases of this two approaches?

1 Answer 1

11

An instance method will have an instance (this) to be called from and operate on. A global-on-Vue function would have Vue itself as its this, which probably means you wouldn't want to use this in it.

So: instance method if it should operate on an instance, global function if it is some sort of utility that doesn't operate on a Vue instance.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.