王磊的个人技术记录 王磊的个人技术记录

记录精彩的程序人生

目录
vue 取消上次请求,避免响应较慢时,多个请求的结果显示错乱
/  

vue 取消上次请求,避免响应较慢时,多个请求的结果显示错乱

import axios from 'axios';

data () {
    return {
        `source:null,`
	}
}

let that = this;
this.cancelQuest();
this.$http.post(this.API, JSON.stringify(target),{cancelToken: new axios.CancelToken(function executor(c) {
                        that.source = c;
                    })})
                    .then((response) => {
                        //TODO 业务处理
                    })
                    .catch(failResponse => {
                        if (axios.isCancel(failResponse)) {
                            console.log('Rquest canceled', failResponse.message); //请求如果被取消,这里是返回取消的message
                        } else {
                            this.$Message.error(failResponse.errorMessage)
                        }
                    });



cancelQuest() {
                if (typeof this.source === 'function') {
                    this.source('终止请求'); //取消请求
                }


标题:vue 取消上次请求,避免响应较慢时,多个请求的结果显示错乱
作者:wanglei03
地址:https://wangleijava.com/articles/2020/01/13/1578909204666.html