您好,欢迎访问一九零五行业门户网

怎么设置axios的全局请求参数

这次给大家带来怎么设置axios的全局请求参数,设置axios全局请求参数的注意事项有哪些,下面就是实战案例,一起来看一下。
应用场景:
1,每个请求都带上的参数,比如token,时间戳等。
2,对返回的状态进行判断,比如token是否过期
代码如下:
axios.interceptors.request.use( config => { var xtoken = getxtoken() if(xtoken != null){ config.headers['x-token'] = xtoken } 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 },function(error){ return promise.reject(error) } ) axios.interceptors.response.use(function (response) { // token 已过期,重定向到登录页面 if (response.data.code == 4){ localstorage.clear() router.replace({             path: '/signin',             query: {redirect: router.currentroute.fullpath}           }) } return response }, function (error) { // do something with response error return promise.reject(error) })
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
vue项目怎样通过百度的bae发布
为什么vue2中不能使用axios http请求
以上就是怎么设置axios的全局请求参数的详细内容。
其它类似信息

推荐信息