方法一、在main.js里直接写函数
//main.js中
Vue.prototype.calculateFloat = function(num,count,type=1){ //calculateFloat 是函数名
alert('这是自定义的公共函数')
}
//在组件中调用
this.calculateFloat()
方法二、新建公共函数js文件挂在到main.js
例:在main.js同级新建commonFun.js文件
exports.install = function (Vue,options){
Vue.prototype.calculateFloat = function(num,count,type=1){
console.log(num)
console.log(count)
console.log(type)
}
}
然后在main.js中挂载
import commonFun from './commonFun'
Vue.use(commonFun)
组件中调用,跟方法一一样