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


基础教程推荐
猜你喜欢
- 解决Android Studio突然不显示logcat日志的问题 2023-02-04
- iOS开发教程之XLForm的基本使用方法 2023-05-01
- Android开发使用RecyclerView添加点击事件实例详解 2023-06-15
- IOS 播放系统提示音使用总结(AudioToolbox) 2023-03-01
- IOS应用内跳转系统设置相关界面的方法 2022-11-20
- Android多返回栈技术 2023-04-15
- Android中的webview监听每次URL变化实例 2023-01-23
- Flutter手势密码的实现示例(附demo) 2023-04-11
- Flutter绘图组件之CustomPaint使用详解 2023-05-12
- android studio按钮监听的5种方法实例详解 2023-01-12