axios的取消请求有两种方式 这次只用了source()来做例子
结合ES6的 new Promise来cancel请求
methods:{
//promise实例
httpGet(url){
return new Promise((resolve,reject) =>{
this.$http.get(url,{
cancelToken:this.cancelRequest.token
}).then(res =>{
// xxxxxx你要搞的事
resolve(res);
}).catch(err =>{
reject(err);
})
})
},
//使用httpGet()
cancelRes(){
this.cancelRequest = this.$http.CancelToken.source();
this.httpGet(你的请求地址).then(res=>{
//xxxxx
}).catch(err =>{
//xxxx
})
}
},
mounted(){
this.cancelRes();
//定时取消请求
let that = this;
setTimeout(function(){
that.cancelRequest.cancel();
},50)
}