【完美解决】vue+elementUI+axios发送请求出现遮罩层的闪烁问题

思路:利用css3动画,关键在于loading中的customClass中加一个样式,当出现loading的时候让它进行一次过渡如下代码

css代码

.axios-loading,
.axios-loading1{animation:axiosloading 1s;}
.axios-loading2{animation:axiosloading 1s;}
.axios-loading3{animation:axiosloading 2s;}
.axios-loading4{animation:axiosloading 3s;}
.axios-loading5{animation:axiosloading 4s;}
@keyframes axiosloading{
  0%{opacity: 0;}
  99% {opacity: 0;}
  100% {opacity: 1;}
}}

js代码

/*axios请求拦截*/
var loading;
/*开启遮罩*/
function startLoading(showLoadingTime) {
  showLoadingTime=showLoadingTime||"2"

  loading = Loading.service({
    customClass:"axios-loading"+showLoadingTime,
    lock: true,
    text: '加载中……',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)'
  })
}
/*关闭遮罩*/
function endLoading() {
  if (loading) {
    loading.close()
  }
}
var starTimestamp = (new Date()).valueOf();
var endTimestamp = (new Date()).valueOf();
/*showLoadingTime请求几秒未返回值显示Loading*/
axios.interceptors.request.use(function (config) {
  if (config.showLoading) {
    /*开启遮罩*/
    starTimestamp = (new Date()).valueOf();
    startLoading(config.showLoadingTime);
  }
  /*接口请求时间长的时候有个遮罩层*/
  if (!config.headers.Authorization) {
    if (window.userId && window.NickName) {
      config.headers.Authorization = Encrypt(window.userId + "|" + (window.NickName ? window.NickName : ""))
    } else {
      config.headers.Authorization = ""
    }
  }
  return config;
}



axios.interceptors.response.use(function (response) {
  
  if (response.config.showLoading) {
    /*关闭遮罩层*/
    if (loading) endLoading();
  }
})

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以先在el-form的submit方法中使用axios发送请求,具体的方法如下: 在template中: ``` <el-form ref="form" :model="form" :rules="rules" label-width="120px"> <el-form-item label="用户名" prop="username"> <el-input v-model="form.username"></el-input> </el-form-item> <el-form-item label="密码" prop="password"> <el-input type="password" v-model="form.password"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm">提交</el-button> <el-button @click="resetForm">重置</el-button> </el-form-item> </el-form> ``` 在script中: ``` import axios from 'axios'; export default { data() { return { form: { username: '', password: '' }, rules: { username: [{ required: true, message: '请输入用户名', trigger: 'blur' }], password: [{ required: true, message: '请输入密码', trigger: 'blur' }] } }; }, methods: { submitForm() { this.$refs.form.validate(valid => { if (valid) { axios.post('/api/login', this.form).then(response => { if (response.data.code === 200) { // 登录成功,跳转到首页 this.$router.push('/home'); } else { this.$message.error('登录失败'); } }); } else { return false; } }); }, resetForm() { this.$refs.form.resetFields(); } } }; ``` 在上述代码中,我们先引入了axios,使用axios发送POST请求到后台的/api/login接口,参数为表单中的form数据。如果接口返回的状态码为200,说明登录成功,此时可以跳转到首页。如果接口返回的状态码不为200,说明登录失败,此时可以给用户提示登录失败信息。 至此,vueelement-ui的form组件与axios的api接口就配合使用成功了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值