第一步:创建 common.js 文件
export default {
getRes(vueObject){
let result = "全局方法";
return result;
}
};
第二步:全局引入
在 main.js 中引入common.js文件
import common from "./commFunction/common";
Vue.prototype.common = common;
//Vue.prototype 可以用来添加一些全局的属性或方法,使其在每个 Vue 实例中都可用。
//Vue.prototype 可以添加任何类型的属性或方法,比如:常量、对象、方法等。
局部引入
import common from './commFunction/common'
第三步:在 .vue 页面中使用
全局引入使用
let res = this.commont.getRes();
console.log(res); // "全局方法"
局部引用使用
commont.getRes(this);