时间:2021-07-01 10:21:17 帮助过:4人阅读
应用场景:
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中实现图片和文件上传的示例代码
Vue实现搜索 和新闻列表功能简单范例
在vue组件中使用axios的方法
以上就是通过axios全局请求参数设置请求以及返回拦截器操作步骤有哪些?的详细内容,更多请关注Gxl网其它相关文章!