来回切换axios取消上一次请求,版本0.22

 

method的方法

 

getCateApi

 

 

 

复制代码
async getCateApi(){
            // 用户快速切换的时候取消上一个请求,要不然太卡了
            const axiosSource = this.axios.CancelToken.source();
            this.cancelRequest = { cancel: axiosSource.cancel };
             await this.axios.post('/h5/categoriesZoneV/'+this.id,qs.stringify({
                field:this.field,
                type:this.type,
                page:this.page,
                perNum:this.perNum,
            }),{
                 cancelToken: axiosSource.token,
                 headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            }).then((response) => {
                // 10个10个添加进去
                response.data.data.forEach(item=>{
                    this.dataArray.push(item);
                })
                this.perNum+=1;
                if (this.dataArray.length >= response.data.total) {
                    this.finished = true;
                }
                 // if(this.dataArray.length==0){
                 //     this.empty=false;
                 //     this.finished = true;
                 // }
                console.log( this.dataArray)
                console.log(response.data.total,this.dataArray.length)
                console.log(response.data)

            }).catch(function (error){
                console.log(error,'err')
            });
        },
复制代码

 

还有三个 cancel, logResponseErrors, clearOldRequest

复制代码
cancel:function (){
            this.cancelRequest.cancel();
            this.clearOldRequest("Cancelled");
        },
        logResponseErrors(err) {
            if (this.axios.isCancel(err)) {
                console.log("Request cancelled");
            }
        },
        clearOldRequest(msg) {
            this.cancelRequest = null;
        },
复制代码

 

 

 

 

 

data的定义

// 取消上一次请求
   cancelRequest: null,

 

 

 

使用

来回切换

 

 

 

 

 

 

效果

 

posted @   漫漫长路</>  阅读(55)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示