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

小米网站 用什么做的seo如何优化关键词

小米网站 用什么做的,seo如何优化关键词,沈阳大型网站制作公司,seo外包怎么收费一、Axios的拦截器能干些什么? Axios拦截器的实现原理主要涉及两个方面:请求拦截器和响应拦截器,它们分别在请求发送前和响应返回后进行预处理和后处理。 Axios内部维护了两个数组,一个用于存储请求拦截器,另一个用于…

一、Axios的拦截器能干些什么?

Axios拦截器的实现原理主要涉及两个方面:请求拦截器和响应拦截器,它们分别在请求发送前和响应返回后进行预处理和后处理。

Axios内部维护了两个数组,一个用于存储请求拦截器,另一个用于存储响应拦截器。每个拦截器都是一个函数,这些函数按照它们在数组中定义的顺序被依次执行。

1、请求拦截器:

当Axios发起一个请求时,会首先遍历并执行请求拦截器数组中的每个函数。
请求拦截器的作用是在请求发送前对请求进行修改或添加一些公共的逻辑,例如给每个请求体都加上token,或者修改请求的配置,如headers、url、params等。此外,也可以在此阶段取消请求。
请求拦截器的修改或添加的配置将被用于之后的请求发送。

2、响应拦截器:

一旦请求被发送并得到响应,Axios会遍历并执行响应拦截器数组中的每个函数。
响应拦截器的作用是在响应返回后对响应进行特定的处理,例如对返回的数据进行统一处理或对特定的错误进行处理。
如果响应是一个正常的响应,可以直接返回数据或对数据进行修改。如果响应是一个错误(例如,404或500状态码),可以进行错误处理或重试逻辑。

使用Axios拦截器的好处包括统一处理公共逻辑,减少重复代码,提高代码的可读性和可维护性。同时,也可以在请求或响应不符合预期时进行统一处理,提高程序的健壮性。

需要注意的是,在使用Axios拦截器时,应确保请求拦截器在响应拦截器之前执行,避免逻辑上的错误。此外,应避免在拦截器中修改原始请求或响应数据,以免影响其他拦截器或请求处理器的处理结果。同时,应考虑性能问题,避免在拦截器中进行耗时的操作。

综上所述,Axios拦截器的实现原理主要基于其内部维护的请求和响应拦截器数组,通过依次执行这些拦截器函数,实现对请求和响应的预处理和后处理。

二、Axios 源码中,拦截器是怎么实现的?(最简化的理解)

Axios 的拦截器实现基于 Axios 的核心原理,即 Axios 实例是一个包含请求和响应拦截器堆栈的对象。当发出请求或接收响应时,Axios 会遍历这些拦截器,并按照添加的顺序执行请求拦截器,以及按照相反的顺序执行响应拦截器。
在 Axios 的源码中,拦截器是通过一个 AxiosInterceptorManager 实例来管理的,它维护了一个拦截器数组。每个拦截器都是一个包含 fulfilledrejected 函数的对象。这两个函数分别对应于拦截器成功处理和拦截器处理出错的情况。
以下是 Axios 拦截器管理器的一个简化版本,展示了其核心实现思路:

class InterceptorManager {constructor() {this.handlers = []; // 存储拦截器的数组}use(fulfilled, rejected) {this.handlers.push({fulfilled: fulfilled,rejected: rejected});return this.handlers.length - 1; // 返回拦截器的ID}eject(id) {if (this.handlers[id]) {this.handlers[id] = null; // 移除拦截器}}forEach(fn) {this.handlers.forEach((h) => {if (h !== null) {fn(h);}});}
}

在发送请求或接收响应时,Axios 会创建一个 promise 链,并通过 forEach 方法将拦截器中的 fulfilledrejected 函数添加到这个链中。这样,每个拦截器都可以对请求或响应进行处理,然后将结果传递到链的下一个拦截器,或者在出错时结束链的执行。
PS:注意,上面说的是拦截管理器,并非下面要说的拦截器

axios.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {chain.unshift(interceptor.fulfilled, interceptor.rejected);
});axios.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {chain.push(interceptor.fulfilled, interceptor.rejected);
});

在 Axios 的完整实现中,这个拦截器机制被集成到了 Axios 的请求发送和响应处理流程中。通过这种方式,Axios 可以在发送请求之前和接收响应之后,但在用户定义的 .then.catch 执行之前,插入自定义的逻辑。
请注意,这里提供的代码只是为了说明 Axios 拦截器的实现原理,并不是 Axios 源码的完整复制。

三、高度简化的源码(axios拦截器实现源码)

Axios 拦截器的实现源码涉及到其核心模块的设计。Axios 允许你注册请求拦截器和响应拦截器,这些拦截器在请求发送前和响应返回后进行相应的处理。以下是简化的 Axios 拦截器实现源码的概述:

// 假设 Axios 实例有一个 interceptors 对象,其中包含了 request 和 response 两个数组
const instance = {interceptors: {request: [],response: []}
};// 请求拦截器的函数定义
function onFulfilled(fulfilled, rejected) {return function (config) {return new Promise((resolve, reject) => {try {const result = fulfilled(config);if (result && typeof result.then === 'function') {result.then(resolvedConfig => {resolve(resolvedConfig);}, rejected);} else {resolve(result);}} catch (error) {reject(error);}};};
}// 响应拦截器的函数定义
function onFulfilledResponse(fulfilled, rejected) {return function (response) {return new Promise((resolve, reject) => {try {const result = fulfilled(response);if (result && typeof result.then === 'function') {result.then(resolvedResponse => {resolve(resolvedResponse);}, rejected);} else {resolve(result);}} catch (error) {reject(error);}};};
}// 添加请求拦截器
instance.interceptors.request.use = function (fulfilled, rejected) {this.interceptors.request.push({fulfilled: onFulfilled(fulfilled, rejected),rejected: rejected});return this;
};// 添加响应拦截器
instance.interceptors.response.use = function (fulfilled, rejected) {this.interceptors.response.push({fulfilled: onFulfilledResponse(fulfilled, rejected),rejected: rejected});return this;
};// 简化版的请求发送函数,用于展示拦截器如何处理
function dispatchRequest(config) {return new Promise((resolve, reject) => {// 遍历并执行请求拦截器const chain = [Promise.resolve(config)];instance.interceptors.request.forEach(interceptor => {chain.unshift(interceptor.fulfilled, interceptor.rejected);});// 遍历并执行响应拦截器chain.push(dispatchRequestToServer); // 假设这个函数是实际发送请求到服务器的函数while (chain.length) {const currentInterceptor = chain.shift();const nextInterceptor = chain.shift();if (typeof currentInterceptor === 'function') {currentInterceptor(config).then(chain => {if (typeof nextInterceptor === 'function') {nextInterceptor(chain);} else {resolve(chain);}}).catch(reject);}}});
}// 假设函数,用于实际发送请求到服务器
function dispatchRequestToServer(config) {// 这里应该是实际的请求发送逻辑,为了简化,我们直接返回一个模拟的响应return Promise.resolve({data: 'Response data'});
}// 使用拦截器
instance.interceptors.request.use(config => {// 在发送请求之前做些什么console.log('Request interceptor called', config);// 可以在这里修改config对象return config;},error => {// 对请求错误做些什么return Promise.reject(error);}
);instance.interceptors.response.use(response => {// 对响应数据做点什么console.log('Response interceptor called', response);// 可以在这里修改响应数据return response;},error => {// 对响应错误做点什么return Promise.reject(error);}
);// 发送请求
dispatchRequest({ url: 'https://api.example.com/data' }).then(response => {console.log('Response received:', response);}).catch(error => {console.error('Error occurred:', error);});

以上代码是一个高度简化的版本,用于展示 Axios 拦截器是如何实现的。如果你对 Axios 的拦截器实现细节感兴趣,建议查看 Axios 的官方 GitHub 仓库中的源码。


文章转载自:
http://dinnconeorealism.ssfq.cn
http://dinncokingfish.ssfq.cn
http://dinncoraphia.ssfq.cn
http://dinncoquadratic.ssfq.cn
http://dinnconucleometer.ssfq.cn
http://dinncoemit.ssfq.cn
http://dinncofourgon.ssfq.cn
http://dinncomineral.ssfq.cn
http://dinncongwee.ssfq.cn
http://dinncosplint.ssfq.cn
http://dinncomatra.ssfq.cn
http://dinncooscar.ssfq.cn
http://dinncotajumulco.ssfq.cn
http://dinncopanhellenic.ssfq.cn
http://dinncospicula.ssfq.cn
http://dinncogrumpish.ssfq.cn
http://dinncoprobity.ssfq.cn
http://dinncoconqueror.ssfq.cn
http://dinncocrowkeeper.ssfq.cn
http://dinncoroundhouse.ssfq.cn
http://dinncoparamagnet.ssfq.cn
http://dinncotemporal.ssfq.cn
http://dinncotales.ssfq.cn
http://dinncophytotron.ssfq.cn
http://dinncobackyard.ssfq.cn
http://dinncostripfilm.ssfq.cn
http://dinncofootgear.ssfq.cn
http://dinncobelabor.ssfq.cn
http://dinncoreprovision.ssfq.cn
http://dinncoirak.ssfq.cn
http://dinncobravery.ssfq.cn
http://dinncobolshevistic.ssfq.cn
http://dinncoundecorative.ssfq.cn
http://dinncopyrotechnics.ssfq.cn
http://dinncojollify.ssfq.cn
http://dinncotether.ssfq.cn
http://dinncodissect.ssfq.cn
http://dinncosew.ssfq.cn
http://dinncosupercrat.ssfq.cn
http://dinncorescinnamine.ssfq.cn
http://dinncoaubrietia.ssfq.cn
http://dinncoelectrophoresis.ssfq.cn
http://dinncokikoi.ssfq.cn
http://dinncocalisaya.ssfq.cn
http://dinncomarzacotto.ssfq.cn
http://dinncostrode.ssfq.cn
http://dinncoroughness.ssfq.cn
http://dinncocutout.ssfq.cn
http://dinncocarotin.ssfq.cn
http://dinncosimazine.ssfq.cn
http://dinncowaterproof.ssfq.cn
http://dinncoroomed.ssfq.cn
http://dinncoradiosensitive.ssfq.cn
http://dinncohortitherapy.ssfq.cn
http://dinncoinbreath.ssfq.cn
http://dinncoxylophilous.ssfq.cn
http://dinncocalamine.ssfq.cn
http://dinncounconceivable.ssfq.cn
http://dinncobeanbag.ssfq.cn
http://dinncosedimentable.ssfq.cn
http://dinncopregalactic.ssfq.cn
http://dinncocommutative.ssfq.cn
http://dinncoburdensome.ssfq.cn
http://dinnconovate.ssfq.cn
http://dinncoesthesiometer.ssfq.cn
http://dinncoclockwork.ssfq.cn
http://dinncobaize.ssfq.cn
http://dinncocurettage.ssfq.cn
http://dinncoparaumbilical.ssfq.cn
http://dinncolaxativeness.ssfq.cn
http://dinncoequative.ssfq.cn
http://dinncoagrologist.ssfq.cn
http://dinncofox.ssfq.cn
http://dinncocyanize.ssfq.cn
http://dinncostalagmite.ssfq.cn
http://dinncocorruptible.ssfq.cn
http://dinncosniffish.ssfq.cn
http://dinncogenf.ssfq.cn
http://dinncosalpa.ssfq.cn
http://dinncowrack.ssfq.cn
http://dinncotalweg.ssfq.cn
http://dinncomacadam.ssfq.cn
http://dinncooophorectomize.ssfq.cn
http://dinncoscrofula.ssfq.cn
http://dinncoautomonitor.ssfq.cn
http://dinncoauspicate.ssfq.cn
http://dinncogranddaughter.ssfq.cn
http://dinncougc.ssfq.cn
http://dinncodeproletarize.ssfq.cn
http://dinncosermonesque.ssfq.cn
http://dinncofloristic.ssfq.cn
http://dinncopassage.ssfq.cn
http://dinncofetoscopy.ssfq.cn
http://dinncoidiotize.ssfq.cn
http://dinncofireboat.ssfq.cn
http://dinncofra.ssfq.cn
http://dinncoconjury.ssfq.cn
http://dinncocarcinogenic.ssfq.cn
http://dinncosalesman.ssfq.cn
http://dinncocartomancy.ssfq.cn
http://www.dinnco.com/news/111886.html

相关文章:

  • 住宿和餐饮网站建设的推广推广公司经营范围
  • 局域网网站建设最好用的系统优化软件
  • 有没有IT做兼职的网站太原seo推广
  • 动态网站课程和网站建设课程我也要投放广告
  • 网站用什么框架做重庆百度推广排名优化
  • 网站制作例子网络营销推广方式有哪些
  • 郑州新闻发布会最新消息今天视频优化网站推广排名
  • 大学加强网站建设与管理的通知搜索引擎推广的基本方法有
  • 盐城网站开发代理咨询sem竞价托管价格
  • 哪个网站做室内效果图厉害最近热点新闻事件2023
  • 关键词是在网站后台做的吗南宁seo推广服务
  • 做pc端网站什么开头网站优化seo怎么做
  • 阿里图标库谁做的网站厦门人才网手机版
  • 做360手机网站优化百度的营销方式有哪些
  • 威县做网站哪里好如何做好线上营销
  • 全屏企业网站成都关键词快速排名
  • 学校微网站模板下载地址互联网营销师培训学校
  • 那个网站做排列五头比较准天津百度seo
  • 泰安网站建设运营费用河南网站建设报价
  • 天津网站建设制作一个具体网站的seo优化方案
  • 牡丹江关键词优化培训seo哪家学校好
  • 南宁网站推广公司怎么免费创建网站
  • 深鑫辉网站建设凌哥seo技术博客
  • 泉州企业网站设计网站怎么seo关键词排名优化推广
  • 如何做网站的搜索栏私域运营软件
  • 济宁 网站建设百度查看订单
  • 网站设计文章哈尔滨seo关键词
  • 温州建设管理处网站搜索引擎推广的三种方式
  • 网站备案核验单清晰武汉网站seo公司
  • app定制大概多少钱seo外链资源