1. 程式人生 > >vue自定義全域性公共函式

vue自定義全域性公共函式

  1. 方法1

在main.js裡進行全域性註冊

Vue.prototype.ajax = function (){}

在所有元件裡可呼叫 this.ajax()

  1. 方法2
// 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