use strictimport Vue from vueimport axios from axiosimport Util from ../assets/js/util.js// Full config: https://github.com/axios/axios#request-config// axios.defaults.baseURL = process.en...
'use strict'
import Vue from 'vue'
import axios from 'axios'
import Util from '../assets/js/util.js'
// Full config: https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// let config = {
// // baseURL: process.env.baseURL || process.env.apiUrl || ""
// // timeout: 60 * 1000, // Timeout
// // withCredentials: true, // Check cross-site Access-Control
// }
//const _axios = axios.create(config)
// Axios拦截器
axios.interceptors.request.use(
// Do something before request is sent
config => {
/*时间设定*/
config.timeout = 200000
/*允许跨域*/
config.withCredentials = true
if (config.headers['Content-Type'] === undefined) {
config.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
// json -> form data
config['transformRequest'] = [
data => {
return Util.jsonToString(data)
}
]
}
// 判断是否为本地测试环境,如果是增加token
if (process.env.NODE_ENV === 'development') {
config.headers.common['token'] =
'TGT-78-5ezn4skRcgX6Z72bSoGM42HxWcLVy05ANdrVvg6eMcqmnq0l4v-cas01.example.org'
}
return config
},
error => {
if (Util.isEmpty(error.errorMessage)) {
Object.assign(error, { errorMessage: '系统异常!' })
}
return Promise.reject(error)
}
)
/*响应拦截器*/
axios.interceptors.response.use(
// Do something with response data
response => {
if (Object.is(response.data.state, 'OK')) {
return Object.assign({ success: true }, response)
} else {
return Object.assign({ success: false }, response)
}
},
error => {
if (Util.isEmpty(error.errorMessage)) {
Object.assign(error, { errorMessage: '系统异常!' })
}
return Promise.reject(error)
}
)
Plugin.install = function(Vue) {
Vue.axios = axios
window.axios = axios
Object.defineProperties(Vue.prototype, {
axios: {
get() {
return axios
}
},
$axios: {
get() {
return axios
}
}
})
}
Vue.use(Plugin)
export default Plugin
沃梦达教程
本文标题为:移动:axios整体理解


猜你喜欢
- Flutter实现底部和顶部导航栏 2022-08-31
- iOS 对当前webView进行截屏的方法 2023-03-01
- 作为iOS开发,这道面试题你能答出来,说明你基础很OK! 2023-09-14
- Android studio实现动态背景页面 2023-05-23
- Android MaterialButton使用实例详解(告别shape、selector) 2023-06-16
- 最好用的ios数据恢复软件:PhoneRescue for Mac 2023-09-14
- Android实现监听音量的变化 2023-03-30
- SurfaceView播放视频发送弹幕并实现滚动歌词 2023-01-02
- Android实现轮询的三种方式 2023-02-17
- 详解flutter engine 那些没被释放的东西 2022-12-04