Axios encapsulation
The import
import axios from 'axios'
import { message } from 'elemrnt-ui'
Copy the code
Unified request address
Axios.defaults. baseURL = "(can fill in the same part of the interface)"Copy the code
Request time limit
axios.defaults.timeout = 10000
Request interceptor
axios.interceptors.request.use(request => { let token = localStorage.getItem('token'); If (token) {// Check whether the token exists. If so, add token request.headers. Token = token to each HTTP header; } return request}, error => {message.error({message: 'request failed! '}); return Promise.reject(error) })Copy the code
Response interceptor
Axios. Interceptors. Response. Use (response = > {the if (response) headers) token) {/ / to determine whether a token, SetItem ('token', response.headers. Token); } if (response.status === 200) {response.status === 200) {response.status === 200; Error => {message.error(" request error or timeout ") return promise.reject (error)})Copy the code
export
export default axios
Copy the code