axios挂在vue上

1
2
import axios from 'axios'
Vue.prototype.$http = axios

在vue文件中使用

1
2
3
this.$http.get('http://www.baidu.com')
.then((res) => {
})

axios 请求携带头部

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import axios from 'axios';
import { getToken } from '@/utils/auth'
const service = axios.create();
service.defaults.timeout = 5000;
service.defaults.baseURL = 'http://127.0.0.1:8080'
service.interceptors.request.use(
config => {
let token = getToken()
if (token) {
config.headers.Authorization = token
}
return config
},
error => {
return Promise.reject(error)
}
);
service.interceptors.response.use(
response => {
return response
},
error => {
return Promise.resolve(error.response)
}
)
export default service