全局配置遮罩层(VUE Element Ui)

组件中

index.js

import ElementUI from 'element-ui'
import { loadingConfig } from './loadingConfig' // 全局的一个基本参数配置

var loading = null
const loadingShow = () => {
  loading = ElementUI.Loading.service(loadingConfig)
}

const loadingHide = () => {
  loading.close()
}

const loadingObj = {
  loadingShow,
  loadingHide
}

export default loadingObj

loadingConfig.js

// 全局页面跳转是否启用loading
export const routerLoading = true

// 全局api接口调用是否启用loading
export const apiLoading = true

// loading参数配置
export const loadingConfig = {
  lock: false,
  text: 'Loading',
  spinner: 'el-icon-loading',
  background: 'rgba(0, 0, 0, 0.3)'
}

main.js之中,感觉不需要引,但是查的资料写了,先放着

import glo_loading from '@/components/loading/index' // loading组件的简单封装
import { routerLoading } from '@/components/loading/loadingConfig' // 全局的页面跳转loading是否启用

request.js之中

import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'
import router from '@/router'
import glo_loading from '@/components/loading/index' // loading组件的简单封装
import { apiLoading } from '@/components/loading/loadingConfig' // 全局的页面跳转loading是否启用

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  withCredentials: true, // send cookies when cross-domain requests
  timeout: 50000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
    apiLoading ? glo_loading.loadingShow() : ''
    if (store.getters.token) {
      config.headers['utk'] = getToken()
    }
    // console.log(config)
    return config
  },
  error => {
    // do something with request error
    apiLoading ? glo_loading.loadingHide() : ''
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    // 如果是Blob类型,直接返回response
    if (response.data instanceof Blob) {
      apiLoading ? glo_loading.loadingHide() : ''
      return response
    }
    if (response.headers['content-disposition']) {
      apiLoading ? glo_loading.loadingHide() : ''
      return response
    }

    const res = response.data
    // if the custom code is not 20000, it is judged as an error.
    if (res.header.code !== '000000') {
      Message({
        message: res.header.msg || 'Error',
        type: 'error',
        duration: 5 * 1000
      })
      if (res.header.code === 'L00001') {
        router.replace({ path: '/notAuth' })
      }
      if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
        // to re-login
        MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
          confirmButtonText: 'Re-Login',
          cancelButtonText: 'Cancel',
          type: 'warning'
        }).then(() => {
          store.dispatch('user/resetToken').then(() => {
            location.reload()
          })
        })
      }
      apiLoading ? glo_loading.loadingHide() : ''
      return Promise.reject(new Error(res.message || 'Error'))
    } else {
      apiLoading ? glo_loading.loadingHide() : ''
      return res
    }
  },
  error => {
    console.log('err' + error) // for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    apiLoading ? glo_loading.loadingHide() : ''
    return Promise.reject(error)
  }
)

export default service

你可能感兴趣的:(VUE,vue.js,前端,javascript)