首页 前端 正文
  • 前端
  • 本文约439字,阅读需2分钟
  • 805
  • 0

在Vuejs中定义公共函数的2种方法

方法一、在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)

组件中调用,跟方法一一样

评论
更换验证码
友情链接