import axios from 'axios'
import { Loading } from 'element-ui'
axios.defaults.baseURL = 'http://192.168.0.6:3000'
window._server = 'http://192.168.0.6:3000'
// 请求超时时间
axios.defaults.timeout = 10000
// 设置post请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
// loading进度条设置
let globalLoading
function startLoading () {
globalLoading = Loading.service({
lock: true,
text: '加载中…',
background: 'rgba(0, 0, 0, 0.7)'
})
}
function endLoading () {
setTimeout(() => {
globalLoading.close()
}, 2000)
}
let needLoadingRequestCount = 0
export function showFullScreenLoading () {
if (needLoadingRequestCount === 0) {
startLoading()
}
needLoadingRequestCount++
}
export function tryHideFullScreenLoading () {
if (needLoadingRequestCount <= 0) return
needLoadingRequestCount--
if (needLoadingRequestCount === 0) {
endLoading()
}
}
// http request 拦截器
axios.interceptors.request.use(
config => {
showFullScreenLoading()
return config
},
err => {
return Promise.reject(err)
}
)
// 响应拦截器
axios.interceptors.response.use(
response => {
tryHideFullScreenLoading()
return Promise.reject(response)
},
error => {
return Promise.reject(error.response)
}
)
elementUI 全局设置loading
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- import axios from 'axios'; import { Message, Loading } fr...
- 实现全局loading加载分析需求,我们只需要在请求发起的时候开始loading,响应结束的时候关闭loading...