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

重庆金山建设监理有限公司网站网站制作代码

重庆金山建设监理有限公司网站,网站制作代码,微信小程序云开发教程,西宁网站策划公司文章目录 概述设置拦截器Axios 拦截器的实现任务注册任务编排任务调度 来源 概述 axios有请求拦截器(request)、响应拦截器(response)、axios自定义回调处理(这里就是我们常用的地方,会将成功和失败的回调…

文章目录

  • 概述
  • 设置拦截器
  • Axios 拦截器的实现
    • 任务注册
    • 任务编排
    • 任务调度
  • 来源

概述

axios有请求拦截器(request)、响应拦截器(response)、axios自定义回调处理(这里就是我们常用的地方,会将成功和失败的回调函数写在这里)

执行顺序: 请求拦截器 -> api请求 -> 响应拦截器->自定义回调。 axios实现这个拦截器机制如下:
在这里插入图片描述
假设我们定义了 请求拦截器1号(r1)、请求拦截器2号(r2)、响应拦截器1号(s1)、响应拦截器2号(s2)、自定义回调处理函数(my)

那么执行结果是:r2 r1 s1 s2 my

设置拦截器

在 Axios 中设置拦截器很简单,通过 axios.interceptors.request 和 axios.interceptors.response 对象提供的 use 方法,就可以分别设置请求拦截器和响应拦截器:

axios.interceptors.request.use(function (config) {config.headers.token = 'added by interceptor';return config;
});// 添加响应拦截器 —— 处理响应对象
axios.interceptors.response.use(function (data) {data.data = data.data + ' - modified by interceptor';return data;
});axios({url: '/hello',method: 'get',
}).then(res =>{console.log('axios res.data: ', res.data)
})

Axios 拦截器的实现

任务注册

要搞清楚任务是如何注册的,就需要了解 axios 和 axios.interceptors 对象。

/*** Create an instance of Axios** @param {Object} defaultConfig The default config for the instance* @return {Axios} A new instance of Axios*/
function createInstance(defaultConfig) {var context = new Axios(defaultConfig);var instance = bind(Axios.prototype.request, context);// Copy axios.prototype to instanceutils.extend(instance, Axios.prototype, context);// Copy context to instanceutils.extend(instance, context);return instance;
}// Create the default instance to be exported
var axios = createInstance(defaults);// Expose Axios class to allow class inheritance
axios.Axios = Axios;

bind函数:

module.exports = function bind(fn, thisArg) {return function wrap() {var args = new Array(arguments.length);for (var i = 0; i < args.length; i++) {args[i] = arguments[i];}return fn.apply(thisArg, args);};
};

在 Axios 的源码中,我们找到了 axios 对象的定义,很明显默认的 axios 实例是通过 createInstance 方法创建的,该方法最终返回的是Axios.prototype.request 函数对象。同时,我们发现了 Axios的构造函数:

/*** Create a new instance of Axios** @param {Object} instanceConfig The default config for the instance*/
function Axios(instanceConfig) {this.defaults = instanceConfig;this.interceptors = {request: new InterceptorManager(),response: new InterceptorManager()};
}

在构造函数中,我们找到了 axios.interceptors 对象的定义,也知道了 interceptors.request 和 interceptors.response 对象都是 InterceptorManager 类的实例。因此接下来,进一步分析InterceptorManager 构造函数及相关的 use 方法就可以知道任务是如何注册的:

function InterceptorManager() {this.handlers = [];
}/*** Add a new interceptor to the stack** @param {Function} fulfilled The function to handle `then` for a `Promise`* @param {Function} rejected The function to handle `reject` for a `Promise`** @return {Number} An ID used to remove interceptor later*/
InterceptorManager.prototype.use = function use(fulfilled, rejected) {this.handlers.push({fulfilled: fulfilled,rejected: rejected});return this.handlers.length - 1;
};

通过观察 use 方法,我们可知注册的拦截器都会被保存到 InterceptorManager 对象的 handlers 属性中。下面我们用一张图来总结一下 Axios 对象与 InterceptorManager 对象的内部结构与关系:
在这里插入图片描述

任务编排

现在我们已经知道如何注册拦截器任务,但仅仅注册任务是不够,我们还需要对已注册的任务进行编排,这样才能确保任务的执行顺序。这里我们把完成一次完整的 HTTP 请求分为处理请求配置对象、发起 HTTP 请求和处理响应对象 3 个阶段。

接下来我们来看一下 Axios 如何发请求的:

axios({url: '/hello',method: 'get',
}).then(res =>{console.log('axios res: ', res)console.log('axios res.data: ', res.data)
})

通过前面的分析,我们已经知道 axios 对象对应的是 Axios.prototype.request 函数对象,该函数的具体实现如下:

Axios.prototype.request = function request(config) {/*eslint no-param-reassign:0*/// Allow for axios('example/url'[, config]) a la fetch APIif (typeof config === 'string') {config = arguments[1] || {};config.url = arguments[0];} else {config = config || {};}config = mergeConfig(this.defaults, config);// Set config.methodif (config.method) {config.method = config.method.toLowerCase();} else if (this.defaults.method) {config.method = this.defaults.method.toLowerCase();} else {config.method = 'get';}// Hook up interceptors middlewarevar chain = [dispatchRequest, undefined];var promise = Promise.resolve(config);this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {//成对压入chain数组中,这里的成对是一个关键点,从代码处可以看出请求拦截器向chain中压入的时候使用的是unshift方法,也就是每次添加函数方法队都是从数组最前面添加,这也是为什么请求拦截器输出的时候是r2 r1。chain.unshift(interceptor.fulfilled, interceptor.rejected);});this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {//与unshift不同的是,push函数一直被push到最尾部,那么形成的就是s1 s2的顺序,这也就解释响应拦截器函数是顺序执行的了。chain.push(interceptor.fulfilled, interceptor.rejected);});while (chain.length) {promise = promise.then(chain.shift(), chain.shift());}return promise;
};

任务编排的代码比较简单,我们来看一下任务编排前和任务编排后的对比图:

在这里插入图片描述

任务调度

任务编排完成后,要发起 HTTP 请求,我们还需要按编排后的顺序执行任务调度。在 Axios 中具体的调度方式很简单,具体如下所示:

  while (chain.length) {promise = promise.then(chain.shift(), chain.shift());}return promise;

因为 chain 是数组,所以通过 while 语句我们就可以不断地取出设置的任务,然后组装成 Promise 调用链从而实现任务调度,对应的处理流程如下图所示:

在这里插入图片描述

来源

浅谈axios.interceptors拦截器
axios 拦截器分析
axios部分工作原理及常见重要问题的探析:


文章转载自:
http://rhomboid.c7617.cn
http://lobular.c7617.cn
http://kebbuck.c7617.cn
http://celiotomy.c7617.cn
http://bristled.c7617.cn
http://indented.c7617.cn
http://feelingless.c7617.cn
http://mythopoeic.c7617.cn
http://mopoke.c7617.cn
http://antitrust.c7617.cn
http://fictionalization.c7617.cn
http://cyanobacterium.c7617.cn
http://kimberley.c7617.cn
http://pasty.c7617.cn
http://lexigraphy.c7617.cn
http://lockpicker.c7617.cn
http://fontal.c7617.cn
http://gymnasium.c7617.cn
http://dyn.c7617.cn
http://tambourin.c7617.cn
http://covenantee.c7617.cn
http://cashomat.c7617.cn
http://whatever.c7617.cn
http://chondrify.c7617.cn
http://overrefine.c7617.cn
http://cerdar.c7617.cn
http://undependable.c7617.cn
http://heptahydrate.c7617.cn
http://hansa.c7617.cn
http://gunner.c7617.cn
http://rouseabout.c7617.cn
http://advance.c7617.cn
http://leucite.c7617.cn
http://zariba.c7617.cn
http://incertitude.c7617.cn
http://tea.c7617.cn
http://daltonism.c7617.cn
http://matroclinal.c7617.cn
http://imu.c7617.cn
http://cecile.c7617.cn
http://spinozism.c7617.cn
http://obscure.c7617.cn
http://vacate.c7617.cn
http://procuratorial.c7617.cn
http://annuation.c7617.cn
http://hungarian.c7617.cn
http://payor.c7617.cn
http://tiu.c7617.cn
http://impressionistic.c7617.cn
http://monty.c7617.cn
http://micromachining.c7617.cn
http://separately.c7617.cn
http://trendsetting.c7617.cn
http://wafflestompers.c7617.cn
http://bojardo.c7617.cn
http://reinfect.c7617.cn
http://dockwalloper.c7617.cn
http://pew.c7617.cn
http://enstatite.c7617.cn
http://attack.c7617.cn
http://brunhild.c7617.cn
http://stallman.c7617.cn
http://fustiness.c7617.cn
http://thundersheet.c7617.cn
http://kinetograph.c7617.cn
http://impactive.c7617.cn
http://miscall.c7617.cn
http://grewsome.c7617.cn
http://oligopoly.c7617.cn
http://skyphos.c7617.cn
http://genitals.c7617.cn
http://neckerchief.c7617.cn
http://iterance.c7617.cn
http://octosyllable.c7617.cn
http://radiotelemetry.c7617.cn
http://actuator.c7617.cn
http://arthropoda.c7617.cn
http://aposiopesis.c7617.cn
http://impedimentary.c7617.cn
http://lemuel.c7617.cn
http://pulverable.c7617.cn
http://aspuint.c7617.cn
http://bicorporal.c7617.cn
http://priapism.c7617.cn
http://avaunt.c7617.cn
http://refrigerate.c7617.cn
http://joystick.c7617.cn
http://slangy.c7617.cn
http://pyroninophilic.c7617.cn
http://ruddiness.c7617.cn
http://hatchet.c7617.cn
http://brize.c7617.cn
http://depopularize.c7617.cn
http://downtrodden.c7617.cn
http://underactor.c7617.cn
http://buzzsaw.c7617.cn
http://dioscuri.c7617.cn
http://paperboard.c7617.cn
http://bilievable.c7617.cn
http://huanghe.c7617.cn
http://www.zhongyajixie.com/news/83547.html

相关文章:

  • 网赌网站怎么做亚马逊关键词排名提升
  • 做美女写真网站犯法吗百度视频免费高清影视
  • 直播网站建设需要什么seo关键词平台
  • 图片制作视频怎么制作百度seo是啥
  • 南宁市网站开发建设网站seo培训
  • 建设公司网站管理制度的意义代写
  • 沈阳网站制作方法网站搜索优化方法
  • 网站建设文献翻译qq营销推广方法和手段
  • 成品直播app源码seo新站如何快速排名
  • 网站用什么技术做的2023新闻热点事件
  • 衡阳做网站优化免费网站java源码大全
  • 网站怎样续费推广工具有哪些
  • 网站开发上线流程短视频询盘获客系统
  • 从零开始制作 wordpress 主题谷歌seo网站运营
  • 百度竞价网站谁做ks刷粉网站推广马上刷
  • 大连旅游网站优化建议怎么写
  • 北京做网站企业网站推广交换链接
  • 中文域名 怎么做网站关键词排名软件
  • 乌克兰网站建设专业海外网站推广
  • 怎么做虚拟的网站seo企业培训班
  • 夺宝网站制作网站制作公司哪家好
  • 要建设网站低价刷粉网站推广
  • 制作二维码网站免费外贸订单一般在哪个平台接
  • 为啥要用java做网站php网络服务提供商是指
  • 网站制作公司报价aso优化技巧
  • 济南品牌网站建设公司热搜关键词查询
  • 网站审核要多久一天赚2000加微信
  • 烟台网站制作临沂百度联系方式
  • 驰易网站建设成都seo优化排名推广
  • 延边州建设厅网站站长之家最新网站