没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
试读
2页
我做vue和react的项目都是用的这一套api(时间戳是我新加的),接口,方法分开,便于管理。 sever.js import axios from 'axios' import qs from 'qs' /** * 自定义实例默认值 */ const instance = axios.create({ timeout: 15000, // 请求超时时间 withCredentials: true }) /** * 添加请求拦截器 ,意思就是发起请求接口之前做什么事,一般都会发起加载一个loading */ instance.interceptors.request.use(
资源详情
资源评论
资源推荐
Vue中的封装中的封装api
我做vue和react的项目都是用的这一套api(时间戳是我新加的),接口,方法分开,便于管理。
sever.js
import axios from 'axios'
import qs from 'qs'
/**
* 自定义实例默认值
*/
const instance = axios.create({
timeout: 15000, // 请求超时时间
withCredentials: true
})
/**
* 添加请求拦截器 ,意思就是发起请求接口之前做什么事,一般都会发起加载一个loading
*/
instance.interceptors.request.use(
config => {
//添加时间戳,防止页面缓存
if (config.method == 'post') {
config.data = {
...config.data,
_t: Date.parse(new Date()) / 1000
}
} else if (config.method == 'get') {
config.params = {
_t: Date.parse(new Date()) / 1000,
...config.params
}
}
return config
},
error => {
// 对请求错误做些什么,处理这个错误
// 可以直接处理或者展示出去,toast show()
console.warn(error)
return Promise.reject(error)
}
)
/**
* 添加响应拦截器
*/
instance.interceptors.response.use(
response => {
let res = response.data
return res
},
error => {
return Promise.reject(error)
}
)
/**
* 使用es6中的类,进行简单封装
*/
class ajaxhttp {
// 使用async ... await
static async get (url, params) {
// eslint-disable-next-line no-return-await
return await instance.get(url, {
params: params
})
}
static async post (url, params) {
// eslint-disable-next-line no-return-await
weixin_38673921
- 粉丝: 8
- 资源: 969
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0