方法一 取消上一次请求 cancelRequest();
< script>
import axios from 'axios'
import qs from 'qs'
export default {
methods: {
request ( keyword ) {
var CancelToken = axios. CancelToken
var source = CancelToken. source ( )
this . cancelRequest ( ) ;
axios. post ( url, qs. stringify ( { kw: keyword} ) , {
headers: {
'Content-Type' : 'application/x-www-form-urlencoded' ,
'Accept' : 'application/json'
} ,
cancelToken: new axios. CancelToken ( function executor ( c ) {
that. source = c;
} )
} ) . then ( ( res ) => {
...
} ) . catch ( ( err ) => {
if ( axios. isCancel ( err) ) {
console. log ( 'Rquest canceled' , err. message) ;
} else {
console. log ( err) ;
}
} )
} ,
cancelRequest ( ) {
if ( typeof this . source === 'function' ) {
this . source ( '终止请求' )
}
} ,
}
}
< / script>
请求拦截(配置发送请求的信息)
axios. interceptors. request. use ( function ( config) {
config. headers. Authorization= window. sessionStorage. getItem ( 'token' )
return config;
} , function ( error) {
return Promise . reject ( error) ;
} ) ;
响应拦截(配置请求回来的信息)
axios. interceptors. response. use ( function ( response) {
return response;
} , function ( error) {
return Promise . reject ( error) ;
} ) ;
Vue.prototype.
h
t
t
p
=
a
x
i
o
s
/
/
其
他
页
面
在
使
用
a
x
i
o
s
的
时
候
直
接
t
h
i
s
.
http = axios //其他页面在使用axios的时候直接 this.
h t t p = a x i o s / / 其 他 页 面 在 使 用 a x i o s 的 时 候 直 接 t h i s . http就可以了
方法二
data( ) {
return {
timer: ''
}
}
/ / 请求方法调用之前 取消上次的请求 也可达到目的
clearTimeout( that. timer)
that. timer= setTimeout( function( ) {
Indicator . open( )
that. httpServe. request( API . openAgreePay, 'post' , {
} ,800 )