add axios extend

This commit is contained in:
jiaming 2018-12-07 18:28:38 +08:00
parent 224c4ecd8d
commit d50126e29c
2 changed files with 41 additions and 0 deletions

View File

@ -0,0 +1,38 @@
import Axios from 'axios'
const timeout = 3000
Axios.defaults.timeout = timeout
function interception (fn, methods) {
return (...args) => {
// Request params num
const argsLen = args.length
// GET Request
if (methods === 'get' && argsLen > 1) {
const requerParams = Object.entries(args[1]).map(([key, value]) => {
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
}).join('&')
args[0] += `?${requerParams}`
args.pop()
// POST Request
} else if (methods === 'post' && argsLen > 2) {
args[1] = Object.entries(args[1]).map(([key, value]) => `${key}=${value}`).join('&')
args.pop()
}
return fn.apply(this, args)
}
}
export default function (Vue) {
Vue.prototype.$http = Axios
Vue.prototype.$http.get = interception(Vue.prototype.$http.get, 'get')
Vue.prototype.$http.post = interception(Vue.prototype.$http.post, 'post')
}

View File

@ -2,7 +2,10 @@ import methodsExtend from './methodsExtend'
import canvasExtend from './canvasExtend'
import axiosExtend from './axiosExtend'
export default function (Vue) {
methodsExtend(Vue)
canvasExtend(Vue)
axiosExtend(Vue)
}