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)
但不清楚哪种方法更适合定义全局功能?有人可以解释差异或指出有关这两种方法的不同用例的一些资源吗?
一个实例方法将有一个实例 ( this) 被一个操作调用。global-on-Vue 函数会将Vue自身作为 its this,这可能意味着您不想在其中使用this。
所以:实例方法如果它应该在一个实例上运行,全局函数如果它是某种不在 Vue 实例上运行的实用程序。
| 归档时间: |
|
| 查看次数: |
792 次 |
| 最近记录: |