//global.js
// 定义vue 全局方法 export default { install(Vue, options = {}) { Vue.prototype.$fn1 = function () { console.log(this); //this vue实例对 console.log(‘f1‘) } Vue.prototype.$fn2 = function () { console.log(‘fn2‘); console.log(this); } } }
2.main.js文件注入
// 定义全局方法 import global from ‘./utils/global‘ Vue.use(global)
3.use
handleCommand(command) { if (command === "aboutme") { this.$fn1() this.$fn2() } },
原文:https://www.cnblogs.com/xiaoliziaaa/p/13109979.html