mirror of
https://github.com/PanJiaChen/vue-element-admin.git
synced 2025-08-07 18:25:45 +08:00
接口请求添加通用 loading 样式
This commit is contained in:
parent
81d41c6416
commit
05aea0c610
21
src/utils/loading.js
Normal file
21
src/utils/loading.js
Normal file
@ -0,0 +1,21 @@
|
||||
import { Loading } from 'element-ui'
|
||||
|
||||
let loadingInstance
|
||||
export const loading = {
|
||||
loadingCount: 0,
|
||||
open() {
|
||||
this.loadingCount === 0 && (loadingInstance = Loading.service({
|
||||
lock: true,
|
||||
text: '加载中',
|
||||
spinner: 'el-icon-loading',
|
||||
background: 'rgba(0, 0, 0, 0.7)'
|
||||
}))
|
||||
this.loadingCount++
|
||||
},
|
||||
close() {
|
||||
setTimeout(() => {
|
||||
this.loadingCount--
|
||||
this.loadingCount === 0 && loadingInstance && loadingInstance.close()
|
||||
}, 20)
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ import { MessageBox, Message } from 'element-ui'
|
||||
import store from '@/store'
|
||||
import { getToken } from '@/utils/auth'
|
||||
import router from '@/router'
|
||||
import { loading } from './loading'
|
||||
|
||||
// create an axios instance
|
||||
const service = axios.create({
|
||||
@ -15,6 +16,7 @@ const service = axios.create({
|
||||
service.interceptors.request.use(
|
||||
config => {
|
||||
// do something before request is sent
|
||||
loading.open()
|
||||
|
||||
if (store.getters.token) {
|
||||
// let each request carry token
|
||||
@ -25,6 +27,7 @@ service.interceptors.request.use(
|
||||
return config
|
||||
},
|
||||
error => {
|
||||
loading.close()
|
||||
// do something with request error
|
||||
console.log(error) // for debug
|
||||
return Promise.reject(error)
|
||||
@ -44,6 +47,7 @@ service.interceptors.response.use(
|
||||
* You can also judge the status by HTTP Status Code
|
||||
*/
|
||||
response => {
|
||||
loading.close()
|
||||
const res = response
|
||||
// if the custom code is not 20000, it is judged as an error.
|
||||
if (res.status !== 200) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user