创建js文件elLoading.js
import { Loading as ElLoading } from 'element-ui'
class LoadingConf {
constructor(){
this.loading = null
this.loadingCounter = -1
}
showLoading(config) {
if (config) {
if (config.loading && !this.loading){
this.loading = ElLoading.service({
fullscreen: false,
target: ".custom_loading"
});
}
} else if (!loading) {
this.loading = ElLoading.service({
fullscreen: false,
target: ".custom_loading"
});
}
this.loadingCounter++;
}
closeLoading() {
if (--this.loadingCounter >= 0) return;
if (this.loading) {
if (this.loading) this.loading.close();
this.loading = null;
}
}
}
const elLoadingObj = new LoadingConf()
export default elLoadingObj
在axios中使用
import axios from 'axios'
import elLoadingObj from '~/utils/elLoading.js'
const instance = axios.create()
instance.interceptors.request.use(config => {
if(process.client){
elLoadingObj.showLoading(config)
}
return config
},
err => {
return Promise.reject(err)
}
)
instance.interceptors.response.use(response => {
if(process.client){
elLoadingObj.closeLoading()
}
return response
})
export default instance