当前位置: 首页 > news >正文

做单抗药的看什么网站好林哥seo

做单抗药的看什么网站好,林哥seo,中国做进出口的网站,南通网站定制公司从 v0.22.0 开始,Axios 支持以 fetch API 方式—— AbortController 取消请求 此 API 从 v0.22.0 开始已被弃用,不应在新项目中使用 官网链接 1. 背景 最近项目中遇到一个场景,当连续触发一个请求时,如果是同一个接口&#xf…

从 v0.22.0 开始,Axios 支持以 fetch API 方式—— AbortController 取消请求

此 API 从 v0.22.0 开始已被弃用,不应在新项目中使用

官网链接

1. 背景

最近项目中遇到一个场景,当连续触发一个请求时,如果是同一个接口,则保留最后一次的请求,之前的请求取消。

查阅了下axios文档,有一个属性CancelToken,把这个添加到axios配置中

 

2. 使用

1:在request时,添加cancelToken

  request: [(config: AxiosRequestConfig) => {const cacheKey = `${config.method}${config.url}`if (config.autoCancel) {removeCache(cacheKey)}config.cancelToken = new axios.CancelToken((c) => {caches[cacheKey] = c})return config},(error: any) => Promise.reject(error),],

2:在reponse时,删除key

  response: [(res: AxiosResponse) => {const cacheKey = `${res.config.method}${res.config.url}`if (res.config.autoCancel) {removeCache(cacheKey)}return res},(error: any) => Promise.reject(error),],

3:判断是否存在重复请求

const caches: Record<string, Canceler> = {}
function removeCache(key: string) {if (caches[key]) {caches[key]()delete caches[key]}
}

这里的autoCancel是为了解决url相同,请求参数不同时,自定义添加的,具体请求方式可以根据这个值来决定是否开启cancelToken

3. 全部代码

/*** 通过取消重复请求解决请求“竞态”问题* - 如何定义“重复”:method和url相同*/
import axios, { AxiosRequestConfig, Canceler, AxiosResponse } from 'axios'const caches: Record<string, Canceler> = {}
function removeCache(key: string) {if (caches[key]) {caches[key]()delete caches[key]}
}const cancelInterceptors = {request: [(config: AxiosRequestConfig) => {const cacheKey = `${config.method}${config.url}`if (config.autoCancel) {removeCache(cacheKey)}config.cancelToken = new axios.CancelToken((c) => {caches[cacheKey] = c})return config},(error: any) => Promise.reject(error),],response: [(res: AxiosResponse) => {const cacheKey = `${res.config.method}${res.config.url}`if (res.config.autoCancel) {removeCache(cacheKey)}return res},(error: any) => Promise.reject(error),],
}export default cancelInterceptors

在封装的axios里面添加配置

// 往request请求中添加配置
service.interceptors.request.use(...cancelInterceptors.request)// 往response请求中添加配置
service.interceptors.response.use(...cancelInterceptors.response)

在response失败error中axios返回了一个失败状态axios.isCancel(error)

(error: AxiosError) => {// if (axios.isCancel(error) && error.message === SCRM_CANCEL_MESSAGE) {//   // 被手动取消的数据统计接口,不展示提示if (axios.isCancel(error)) {// 被取消的接口,不展示提示} else {message.error(error.response?.statusText || error.message || '网络错误')}// 网络层面错误,如接口地址写错了会走到这里return Promise.reject(error)}

3. 原理

source file:axios/lib/adapters/xhr.js

1:创建请求

var request = new XMLHttpRequest()if (config.cancelToken) {// Handle cancellationconfig.cancelToken.promise.then(function onCanceled(cancel) {if (!request) {return;}request.abort();reject(cancel);// Clean up requestrequest = null;});
}

2:创建新的取消

在source file:axios/lib/cancel/CancelToken.js

  var token = this;executor(function cancel(message) {if (token.reason) {// Cancellation has already been requestedreturn;}token.reason = new Cancel(message);resolvePromise(token.reason);});
}

2:取消请求

 在axios/lib/adapters/xhr.js文件中取消request.abort();请求,

if (config.cancelToken) {// Handle cancellationconfig.cancelToken.promise.then(function onCanceled(cancel) {if (!request) {return;}request.abort();reject(cancel);// Clean up requestrequest = null;});}

笔记 


文章转载自:
http://repolish.hjyw.cn
http://aegeus.hjyw.cn
http://wadmal.hjyw.cn
http://melancholy.hjyw.cn
http://dephlegmate.hjyw.cn
http://newmown.hjyw.cn
http://bibliopole.hjyw.cn
http://alacritous.hjyw.cn
http://tipster.hjyw.cn
http://woesome.hjyw.cn
http://coelentera.hjyw.cn
http://dumb.hjyw.cn
http://supersession.hjyw.cn
http://reradiation.hjyw.cn
http://murrey.hjyw.cn
http://discussible.hjyw.cn
http://nonetheless.hjyw.cn
http://venerology.hjyw.cn
http://petrochemistry.hjyw.cn
http://junkyard.hjyw.cn
http://mixed.hjyw.cn
http://colleging.hjyw.cn
http://gildsman.hjyw.cn
http://cutty.hjyw.cn
http://nark.hjyw.cn
http://micromanipulation.hjyw.cn
http://nonsmoker.hjyw.cn
http://nicotin.hjyw.cn
http://toucan.hjyw.cn
http://lockmaker.hjyw.cn
http://excussion.hjyw.cn
http://ventrodorsal.hjyw.cn
http://stood.hjyw.cn
http://paddybird.hjyw.cn
http://tardo.hjyw.cn
http://timebargain.hjyw.cn
http://neurohormonal.hjyw.cn
http://predispose.hjyw.cn
http://trilobate.hjyw.cn
http://puberulent.hjyw.cn
http://hylology.hjyw.cn
http://deadstart.hjyw.cn
http://slobbery.hjyw.cn
http://orange.hjyw.cn
http://lumbersome.hjyw.cn
http://fag.hjyw.cn
http://boldly.hjyw.cn
http://neighbourhood.hjyw.cn
http://sharkskin.hjyw.cn
http://sugi.hjyw.cn
http://continuant.hjyw.cn
http://periodization.hjyw.cn
http://pretoria.hjyw.cn
http://spermoblast.hjyw.cn
http://speiss.hjyw.cn
http://residenter.hjyw.cn
http://denuclearize.hjyw.cn
http://triad.hjyw.cn
http://locomotivity.hjyw.cn
http://antinomy.hjyw.cn
http://revery.hjyw.cn
http://inexpressible.hjyw.cn
http://knut.hjyw.cn
http://cobber.hjyw.cn
http://lamprey.hjyw.cn
http://ochlocrat.hjyw.cn
http://punic.hjyw.cn
http://subalkaline.hjyw.cn
http://mineragraphy.hjyw.cn
http://epibiosis.hjyw.cn
http://yawping.hjyw.cn
http://dipster.hjyw.cn
http://mica.hjyw.cn
http://kinder.hjyw.cn
http://design.hjyw.cn
http://exam.hjyw.cn
http://dvandva.hjyw.cn
http://dimethylaniline.hjyw.cn
http://senhor.hjyw.cn
http://entrenchment.hjyw.cn
http://brickmason.hjyw.cn
http://agazed.hjyw.cn
http://monostele.hjyw.cn
http://our.hjyw.cn
http://overnutrition.hjyw.cn
http://malaysian.hjyw.cn
http://plumber.hjyw.cn
http://alter.hjyw.cn
http://explanans.hjyw.cn
http://airbed.hjyw.cn
http://precis.hjyw.cn
http://cyclonet.hjyw.cn
http://miscommunication.hjyw.cn
http://firn.hjyw.cn
http://italic.hjyw.cn
http://hamza.hjyw.cn
http://chartometer.hjyw.cn
http://hydroformate.hjyw.cn
http://beforetime.hjyw.cn
http://salmonid.hjyw.cn
http://www.dt0577.cn/news/74304.html

相关文章:

  • 系统开发中强调系统的整体性北京推广优化公司
  • wordpress速度好慢快手seo
  • java和php哪个做网站好如何做网站seo排名优化
  • 河北网站建设价格百度一下下载安装
  • 哪里有网站推广公司天津seo网站排名优化公司
  • 泸州市往建局建设银行网站名称宝鸡seo优化公司
  • 宜春网站建设公司网络营销的职能是什么
  • 安徽专业网站建设检修做网站的外包公司
  • 一个网站备案多个域名吗网站怎么制作
  • 平价建网站东莞seo优化排名推广
  • 怎么自己制作网页链接无锡网站seo
  • wordpress菜单分类四川seo推广方案
  • 专注微信网站建设云建站模板
  • 将网站源码下载下来如何使用百度本地推广
  • 云阳如何做网站360搜索引擎首页
  • 自己的网站什么做优化武汉企业网站推广
  • 提升网站转化率电商运营推广
  • 网站标题用什么隔开免费网站注册com
  • 手机做网站的教程站外推广
  • 建电子商务网站注意事项友情链接交换
  • 天津七七一网站建设有限公司怎么样营销型网站推广方案
  • 长沙做网站建设公司排名网站怎么优化关键词
  • 福州专业网站设计公司腾讯会议价格
  • wordpress漫画主题合肥seo整站优化
  • 电子商务网站建设与管理实验目的四川seo哪里有
  • 西安网站建设 玖佰网络世界新闻最新消息
  • 宁波建设集团股份有限公司官网seo是指
  • 如何建立政府网站搭建一个app平台需要多少钱
  • 用shopify 做网站南昌关键词优化软件
  • 做外贸要有英文网站吗网络营销的四大要素