vue自定義全局公共函數

單獨零散的函數

在main.js裏進行全局註冊ajax

Vue.prototype.ajax = function (){}函數

在全部組件裏可調用
this.ajax()this

多個函數定義在一個對象裏

// 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
相關文章
相關標籤/搜索