在请求拦截器注入token

This commit is contained in:
jqtmviyu@gmail.com 2021-01-15 17:34:35 +08:00
parent 6b6cbd02a3
commit 1c75f0bb5d
2 changed files with 13 additions and 19 deletions

View File

@ -17,10 +17,11 @@ export function login(data) {
export function getUserInfo() {
return request({
url: '/sys/profile',
method: 'POST',
headers: {
Authorization: `Bearer ${store.getters.token}`
}
method: 'POST'
// headers: {
// Authorization: `Bearer ${store.getters.token}`
// }
// 在请求拦截器注入请求头
})
}

View File

@ -1,3 +1,4 @@
import store from '@/store'
import axios from 'axios'
import { Message } from 'element-ui'
// import { MessageBox, Message } from 'element-ui'
@ -14,21 +15,13 @@ const service = axios.create({
// request interceptor // axios的请求拦截器
service.interceptors.request.use(
config => {
// do something before request is sent
// if (store.getters.token) {
// // let each request carry token
// // ['X-Token'] is a custom headers key
// // please modify it according to the actual situation
// config.headers['X-Token'] = getToken()
// }
return config
}//,
// error => {
// // do something with request error
// console.log(error) // for debug
// return Promise.reject(error)
// }
if (store.getters.token && !config.headers.Authorization) { // 有token并且没有请求头
config.headers.Authorization = `Bearer ${store.getters.token}` // 在请求头添加token
}
}, err => {
Message.error('请求失败')
return Promise.reject(err)
}
)
// response interceptor