在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)

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


element-ui根据表格数据的值,设置单元格的颜色

element Table 多选表格 全选禁用

评 论
更换验证码