vue封装全局loading

首先创建一个loading文件

<template>
    <div>
        <div v-loading.fullscreen.lock="loadingAll" element-loading-background="rgba(0, 0, 0, 0.6)">
        </div>
    </div>
</template>
<script>
export default {
    computed: {
      loadingAll() { return this.$store.state.user.loadingAll}
    }
}
</script>

在store中添加变量

在这里插入图片描述

在app.vue中引入loading文件

在这里插入图片描述

然后就可以使用啦

 //在需要用到的页面添加如下代码
 computed: {
     loadingAll() { return this.$store.state.user.loadingAll}
 },
 methods: {
 	 submitForm() {
 	 	 //提交表单打开loading,响应后关闭
	     this.$store.state.user.loadingAll = true
	      // this.loadingAll = true
	      this.$refs["form"].validate(valid => {
	          if (valid) {
	              if (this.form.id != null) {
	                  updateStructure(this.form).then(response => {
	                      this.msgSuccess("修改成功");
	                      this.open = false;
	                      // this.loadingAll = false
	                      this.$store.state.user.loadingAll = false
	                      this.getList();
	                  });
	              } else {
	                  addStructure(this.form).then(response => {
	                      this.msgSuccess("新增成功");
	                      this.open = false;
	                      // this.loadingAll = true
	                      this.$store.state.user.loadingAll = false
	                      this.getList();
	                  });
	              }
	          }else {
	              this.$store.state.user.loadingAll = false
	              // this.loadingAll = false
	          }
	      });
	  },
 }

全局使用

// request拦截器
service.interceptors.request.use(config => {
  // 是否需要设置 token
  const isToken = (config.headers || {}).isToken === false
  if (getToken() && !isToken) {
    config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  }
  store.state.user.loadingAll = true
  return config
}, error => {
    Promise.reject(error)
})

// 响应拦截器
service.interceptors.response.use(res => {
    store.state.user.loadingAll = false
    // 未设置状态码则默认成功状态
    const code = res.data.code || 200;
    // 获取错误信息
    const msg = errorCode[code] || res.data.msg || errorCode['default']
    if (code === 401) {
      MessageBox.alert('登录状态已过期,请重新登录', '系统提示', {
        confirmButtonText: '确定',
        showClose:false,
        type: 'warning',
        callback: action => {
            store.dispatch('LogOut').then(() => {
                location.href = '/index';
              })
            }
        })
    } else if (code === 500) {
      Message({
        message: msg,
        type: 'error'
      })
      store.state.user.loadingAll = false
      
      return Promise.reject(new Error(msg))
    } else if (code !== 200) {
      Notification.error({
        title: msg
      })
      store.state.user.loadingAll = false
      
      return Promise.reject('error')
    } else {
      return res.data
    }
  },
  error => {
    console.log('err' + error)
    store.state.user.loadingAll = false
    let { message } = error;
    if (message == "Network Error") {
      message = "后端接口连接异常";
    }
    else if (message.includes("timeout")) {
      message = "系统接口请求超时";
    }
    else if (message.includes("Request failed with status code")) {
      message = "系统接口" + message.substr(message.length - 3) + "异常";
    }
    Message({
      message: message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

各位客官要是感觉有用的话能留个小心心嘛,谢谢啦!!!^ v ^

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值