vue自定义全局公共函数

单独零散的函数

在main.js里进行全局注册

Vue.prototype.ajax = function (){}

在所有组件里可调用
this.ajax()

多个函数定义在一个对象里


// xx.js文件
var tools = {}
tools.addNum = function (x, y) {
  return x * y
} // 还可以在这个文件里面添加多个函数
tools.install = function (Vue, options) {
  Vue.prototype.$tools = tools
  Vue.tools = tools
}
export default tools

// main.js
import tools from 'xx'

Vue.use(tools)

// 子组件
let yy = this.$tools.addNum(6, 9)
console.log(yy) // 54

来源:https://segmentfault.com/a/1190000016403356

猜你喜欢

转载自www.cnblogs.com/lovellll/p/10139257.html