import { Loading } from 'element-ui'
var loadingCount = 0
var loading
const loadingOptions = {
lock: true,
text: '加载中...',
background: 'transparent',
fullscreen: true
}
const startLoading = () => {
loading = Loading.service(loadingOptions)
}
const endLoading = () => {
loading.close()
}
export const showLoading = () => {
if (loadingCount === 0) {
startLoading()
}
loadingCount += 1
}
export const hideLoading = () => {
if (loadingCount <= 0) {
return
}
loadingCount -= 1
if (loadingCount === 0) {
endLoading()
}
}
service.interceptors.request.use(
config => {
if (config.isLoad !== false) showLoading()
},
error => {
Promise.reject(error)
}
)
service.interceptors.response.use(function (response) {
hideLoading()
return response
})
返回response或者error的时候隐藏