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整体理解


基础教程推荐
猜你喜欢
- iOS开发 全机型适配解决方法 2023-01-14
- iOS中如何判断当前网络环境是2G/3G/4G/5G/WiFi 2023-06-18
- IOS获取系统相册中照片的示例代码 2023-01-03
- Android Compose自定义TextField实现自定义的输入框 2023-05-13
- Android实现短信验证码输入框 2023-04-29
- Flutter进阶之实现动画效果(三) 2022-10-28
- Android开发Compose集成高德地图实例 2023-06-15
- iOS Crash常规跟踪方法及Bugly集成运用详细介绍 2023-01-18
- MVVMLight项目Model View结构及全局视图模型注入器 2023-05-07
- iOS开发使用XML解析网络数据 2022-11-12