vue+axios实现点击取消请求功能

在这里插入图片描述
在这里插入图片描述

代码片段

<template>
<el-button type="primary" @click="clickExportData">导出</el-button>

<el-dialog title="正在导出,请稍等" :visible.sync="progressShow" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false" width="20%">
       <div style="text-align: center;">
           <el-progress :percentage="percentage" type="circle"></el-progress>
       </div>
       <div slot="footer">
           <el-button type="primary" @click="cancelSend">取消下载</el-button>
       </div>
   </el-dialog>
</template>
<script>
export default {
	data() {
        return {
            percentage:0, //进度条
            progressShow: false, // 是否显示进度条弹出层
            source: ''
        }
    },
    methods: {
		// 点击导出数据
        clickExportData(){
			this.progressShow = true;
            this.percentage = 0;
            let times = setInterval(() => {
                if(this.percentage == 89){
                    clearInterval(times);
                }else{
                    if(!this.percentage){
                        this.percentage = 0;
                    }
                    this.percentage++;
                }
            }, 2000);
            // 每次发送请求前先初始化CancelToken,防止在点击一次后失效的问题
            // this.$axios是在main.js中引入的axios:
            // import axios from "axios";
            // Vue.prototype.$http = axios
            const { CancelToken } = this.$axios; 
            this.source = CancelToken.source();
            this.$http.post(url, data, { cancelToken: this.source.token }).then(res => {			console.log('请求成功');
            }).catch(err => {
            	// 用户取消
                console.log(err);
                this.progressShow = false;
                if(err.message && err.message == '请求被用户取消'){
                    this.$message.error('请求已被取消!');
                }else{
                    this.$message.error('请求超时,导出数据失败!');
                }
            });
		},
		// 点击取消发送请求
        cancelSend(){
			this.source.cancel('请求被用户取消');
		}
	}
}
</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以通过在axios拦截器中进行处理来实现相同请求地址3秒内禁止重复请求功能。具体实现步骤如下: 1. 在axios中创建一个请求队列,用于存储正在请求中的请求地址及其对应的取消函数。 ``` const pending = [] ``` 2. 创建一个axios拦截器,在请求发送前先检查请求队列中是否已经存在相同请求地址的请求。若存在,则判断距离上次请求时间是否超过3秒,若是则允许发送请求,否则取消请求。 ``` axios.interceptors.request.use(config => { // 检查请求队列中是否已经存在相同请求地址的请求 const requestUrl = config.url const index = pending.findIndex(item => item.url === requestUrl) if (index !== -1) { // 存在相同请求地址的请求 const timeDiff = Date.now() - pending[index].time if (timeDiff < 3000) { // 距离上次请求时间不足3秒,取消请求 pending[index].cancel() pending.splice(index, 1) } else { // 距离上次请求时间超过3秒,允许发送请求 pending.splice(index, 1) config.cancelToken = new axios.CancelToken(cancel => { pending.push({ url: requestUrl, time: Date.now(), cancel }) }) } } else { // 不存在相同请求地址的请求,允许发送请求 config.cancelToken = new axios.CancelToken(cancel => { pending.push({ url: requestUrl, time: Date.now(), cancel }) }) } return config }) ``` 3. 创建一个axios拦截器,在请求结束后将请求请求队列中移除。 ``` axios.interceptors.response.use(response => { const requestUrl = response.config.url const index = pending.findIndex(item => item.url === requestUrl) if (index !== -1) { pending.splice(index, 1) } return response }, error => { const requestUrl = error.config.url const index = pending.findIndex(item => item.url === requestUrl) if (index !== -1) { pending.splice(index, 1) } return Promise.reject(error) }) ``` 这样,每次发送请求时都会检查请求队列中是否存在相同请求地址的请求,若存在则判断距离上次请求时间是否超过3秒,若是则允许发送请求,否则取消请求。同时,在请求结束后将请求请求队列中移除,以便下次请求
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值