vue 请求数据头部传入token值
需求:点击退出的发送请求的时候后台要求传Headers中传入token代码如下://第一个{}为所传的data数据,第二个{}为传入headersthis.axios.post('/admin/logout',{},{headers:{//我这里对像值这么写是因为我在上页面计算属性中获取了usr,user:state=>state.user.usertoke...
·
需求:点击退出的发送请求的时候后台要求传Headers中传入token
代码如下:
// 第一个{}为所传的data数据,第二个{}为传入headers
this.axios.post('/admin/logout',{},{
headers:{
//我这里对像值这么写是因为我在上页面计算属性中获取了usr,user:state=>state.user.user
token:this.user.token
}
}).then(res=>{
this.$message('退出成功')
// 调用vuex中方法清除状态和存储
this.$store.commit('logout')
// 返回到登录页
this.$router.push({name:"login"})
})
store中的退出登录的方法
export default {
state:{
user:{},
token:false
},
getters:{
},
mutations:{
// 退出登录
logout(state){
// 清除状态
state.user = {}
state.token = false
// 清除本地存储
window.sessionStorage.clear()
}
},
actions:{
}
}
更多推荐
已为社区贡献39条内容
所有评论(0)