VUE_APP_BASE_URL=https://jsonplaceholder.typicode.com/
import axios from 'axios'
import Nprogress from 'nprogress'
import { Message } from 'element-ui'
Nprogress.configure({ showSpinner: false })
const request = axios.create({
baseURL: process.env.VUE_APP_BASE_URL,
timeout: 12000,
headers: {
'content-Type': 'application/json',
Accept: 'application/json'
}
})
//请求拦截器
request.interceptors.request.use(
config => {
Nprogress.start()
return config
},
error => {
return Promise.reject(error)
}
)
//响应拦截器
request.interceptors.response.use(
response => {
Nprogress.done()
return response
},
error => {
Nprogress.done()
Message.error({
message: error.message
})
return Promise.reject(error)
}
)
<script>
import request from '@/utils/request.js'
export default{
methods: {
test() {
const users = request.get('/users11')
users
.then(res => {
console.log(res)
})
.catch(error => {
console.log(error.message)
})
}
}
</script>