Vue.js 中的全局方法和实例方法有什么区别?

Sho*_*uri 10 vue.js

Vue.js 关于插件的官方文档描述了全局方法和属性以及 Vue 实例方法。

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

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

但不清楚哪种方法更适合定义全局功能?有人可以解释差异或指出有关这两种方法的不同用例的一些资源吗?

Roy*_*y J 7

一个实例方法将有一个实例 ( this) 被一个操作调用。global-on-Vue 函数会将Vue自身作为 its this,这可能意味着您不想在其中使用this

所以:实例方法如果它应该在一个实例上运行,全局函数如果它是某种不在 Vue 实例上运行的实用程序。