组件中
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之中,感觉不需要引,但是查的资料写了,先放着文章来源:https://www.toymoban.com/news/detail-509634.html
import glo_loading from '@/components/loading/index' // loading组件的简单封装
import { routerLoading } from '@/components/loading/loadingConfig' // 全局的页面跳转loading是否启用
request.js之中文章来源地址https://www.toymoban.com/news/detail-509634.html
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 Element Ui)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!