当前位置:   article > 正文

elementUI 全局设置loading_element ui 全局loading

element ui 全局loading
  1. import axios from 'axios'
  2. import { Loading } from 'element-ui'
  3. axios.defaults.baseURL = 'http://192.168.0.6:3000'
  4. window._server = 'http://192.168.0.6:3000'
  5. // 请求超时时间
  6. axios.defaults.timeout = 10000
  7. // 设置post请求头
  8. axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
  9. // loading进度条设置
  10. let globalLoading
  11. function startLoading () {
  12. globalLoading = Loading.service({
  13. lock: true,
  14. text: '加载中…',
  15. background: 'rgba(0, 0, 0, 0.7)'
  16. })
  17. }
  18. function endLoading () {
  19. setTimeout(() => {
  20. globalLoading.close()
  21. }, 2000)
  22. }
  23. let needLoadingRequestCount = 0
  24. export function showFullScreenLoading () {
  25. if (needLoadingRequestCount === 0) {
  26. startLoading()
  27. }
  28. needLoadingRequestCount++
  29. }
  30. export function tryHideFullScreenLoading () {
  31. if (needLoadingRequestCount <= 0) return
  32. needLoadingRequestCount--
  33. if (needLoadingRequestCount === 0) {
  34. endLoading()
  35. }
  36. }
  37. // http request 拦截器
  38. axios.interceptors.request.use(
  39. config => {
  40. showFullScreenLoading()
  41. return config
  42. },
  43. err => {
  44. return Promise.reject(err)
  45. }
  46. )
  47. // 响应拦截器
  48. axios.interceptors.response.use(
  49. response => {
  50. tryHideFullScreenLoading()
  51. return Promise.reject(response)
  52. },
  53. error => {
  54. return Promise.reject(error.response)
  55. }
  56. )
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/227693
推荐阅读
相关标签
  

闽ICP备14008679号