马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1.使用CancelToken:
- class RequestHttp {
- service: AxiosInstance;
- public constructor(config: AxiosRequestConfig) {
- // 实例化axios
- this.service = axios.create(config);
- /**
- * @description 请求拦截器
- * 客户端发送请求 -> [请求拦截器] -> 服务器
- */
- this.service.interceptors.request.use(
- (config: any) => {
- if (!getUrlParams("id")) {
- config["cancelToken"] = new axios.CancelToken(function (cancel) {
- cancel("当前URL中未携带id参数,请求已被取消");
- });
- }
- return config;
- },
- (error: AxiosError) => {
- return Promise.reject(error);
- }
- );
- }
- }
- export default new RequestHttp(config);
复制代码 2.使用AbortController
- class RequestHttp {
- service: AxiosInstance;
- public constructor(config: AxiosRequestConfig) {
- // 实例化axios
- this.service = axios.create(config);
- /**
- * @description 请求拦截器
- * 客户端发送请求 -> [请求拦截器] -> 服务器
- */
- this.service.interceptors.request.use(
- (config: any) => {
- if (!getUrlParams("id")) {
- const abortController = new AbortController();
- config.signal = abortController.signal;
- abortController.abort("当前URL中未携带id参数,请求已被取消");
- }
- return config;
- },
- (error: AxiosError) => {
- return Promise.reject(error);
- }
- );
- }
- }
- export default new RequestHttp(config);
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |