🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# axios封装中间件和避免重复请求 ## 封装的axios中间件 ``` <pre class="calibre13">``` <span class="token">/</span>\<span class="token">*</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span>' \<span class="token">*</span> <span class="token4">'::::::::::::'</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span> \<span class="token">*</span> '<span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span><span class="token1">.</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span><span class="token4">``</span></span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span> \<span class="token">*</span> <span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span><span class="token4">``</span></span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span>' <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token4">' '</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span>' <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token4">' :::: .:::::::'</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token4">' ::::: .:::::::::'</span> '<span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token4">' :::::.:::::::::'</span> '<span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token4">' ::::::::::::::'</span> <span><span class="token4">``</span></span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span class="token">...</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span> <span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span>' <span><span class="token4">``</span></span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span><span class="token4">``</span></span><span><span class="token4">``</span></span><span class="token4">':. '</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span>' <span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span><span class="token1">.</span> \<span class="token">*</span> <span class="token4">'.:::::'</span> <span class="token4">':'</span><span><span class="token4">``</span></span><span><span class="token4">``</span></span><span class="token1">.</span><span class="token1">.</span> \<span class="token">*</span> \<span class="token">*</span> @Author<span class="token1">:</span> 洋宝儿 \<span class="token">*</span> @Date<span class="token1">:</span> <span class="token5">2022</span><span class="token">-</span><span class="token5">02</span><span class="token">-</span><span class="token5">16</span> <span class="token5">10</span><span class="token1">:</span><span class="token5">57</span><span class="token1">:</span><span class="token5">05</span> \<span class="token">*</span> @Description<span class="token1">:</span> 封装axios 自定义配置 \<span class="token">*</span> @FilePath<span class="token1">:</span> \\ViteDemo01\\src\\axiosintrnce\\axiosinterce<span class="token1">.</span>js \<span class="token">*</span><span class="token">/</span> <span class="token2">import</span> axios <span class="token2">from</span> <span class="token4">"axios"</span><span class="token1">;</span> <span class="token7">/\* 开关方式增加了参数 customOptions \*/</span> <span class="token2">function</span> <span class="token3">myAxios</span><span class="token1">(</span>axiosconfig<span class="token1">,</span> customOptions<span class="token1">)</span> <span class="token1">{</span> <span class="token6">// 返回是promise</span> <span class="token2">const</span> service \<span class="token">=</span> axios<span class="token1">.</span><span class="token3">create</span><span class="token1">(</span><span class="token1">{</span> baseURL<span class="token1">:</span> <span class="token4">'http://localhost:8888'</span><span class="token1">,</span> <span class="token6">// 设置统一的请求前缀</span> timeout<span class="token1">:</span> <span class="token5">10000</span><span class="token1">,</span> <span class="token6">// 设置统一的超时时长</span> <span class="token1">}</span><span class="token1">)</span> <span class="token">/</span>\<span class="token">*</span> 采用第二种方式 根据自定义配置 设置是否开启重复请求的开关 \<span class="token">*</span><span class="token">/</span> <span class="token2">let</span> custom\_options \<span class="token">=</span> Object<span class="token1">.</span><span class="token3">assign</span><span class="token1">(</span><span class="token1">{</span> repeat\_request\_cancel<span class="token1">:</span> <span class="token5">true</span><span class="token1">,</span> <span class="token6">// 是否开启取消重复请求, 默认为 true</span> <span class="token1">}</span><span class="token1">,</span> customOptions<span class="token1">)</span><span class="token1">;</span> service<span class="token1">.</span>interceptors<span class="token1">.</span>request<span class="token1">.</span><span class="token3">use</span><span class="token1">(</span> config \<span class="token">=></span> <span class="token1">{</span> <span class="token3">removePending</span><span class="token1">(</span>config<span class="token1">)</span><span class="token1">;</span> <span class="token6">// addPending(config);</span> custom\_options<span class="token1">.</span>repeat\_request\_cancel <span class="token">&&</span> <span class="token3">addPending</span><span class="token1">(</span>config<span class="token1">)</span><span class="token1">;</span> <span class="token6">// 采用第二种方式 开关方式</span> <span class="token2">return</span> config<span class="token1">;</span> <span class="token1">}</span><span class="token1">,</span> error \<span class="token">=></span> <span class="token1">{</span> <span class="token2">return</span> Promise<span class="token1">.</span><span class="token3">reject</span><span class="token1">(</span>error<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">)</span><span class="token1">;</span> service<span class="token1">.</span>interceptors<span class="token1">.</span>response<span class="token1">.</span><span class="token3">use</span><span class="token1">(</span> response \<span class="token">=></span> <span class="token1">{</span> <span class="token3">removePending</span><span class="token1">(</span>response<span class="token1">.</span>config<span class="token1">)</span><span class="token1">;</span> <span class="token2">return</span> response<span class="token1">;</span> <span class="token1">}</span><span class="token1">,</span> error \<span class="token">=></span> <span class="token1">{</span> error<span class="token1">.</span>config <span class="token">&&</span> <span class="token3">removePending</span><span class="token1">(</span>error<span class="token1">.</span>config<span class="token1">)</span><span class="token1">;</span> <span class="token2">return</span> Promise<span class="token1">.</span><span class="token3">reject</span><span class="token1">(</span>error<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">)</span><span class="token1">;</span> <span class="token2">return</span> <span class="token3">service</span><span class="token1">(</span>axiosconfig<span class="token1">)</span> <span class="token1">}</span> <span class="token2">const</span> pendingMap \<span class="token">=</span> <span class="token2">new</span> <span class="token3">Map</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token">/</span>\<span class="token">*</span>\<span class="token">*</span> \<span class="token">*</span> 生成每个请求唯一的键 \<span class="token">*</span> @param <span class="token1">{</span>\<span class="token">*</span><span class="token1">}</span> config \<span class="token">*</span> @returns string \<span class="token">*</span><span class="token">/</span> <span class="token2">function</span> <span class="token3">getPendingKey</span><span class="token1">(</span>config<span class="token1">)</span> <span class="token1">{</span> <span class="token2">let</span> <span class="token1">{</span> url<span class="token1">,</span> method<span class="token1">,</span> params<span class="token1">,</span> data <span class="token1">}</span> \<span class="token">=</span> config<span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span><span class="token2">typeof</span> data \<span class="token">===</span> <span class="token4">'string'</span><span class="token1">)</span> data \<span class="token">=</span> <span class="token5">JSON</span><span class="token1">.</span><span class="token3">parse</span><span class="token1">(</span>data<span class="token1">)</span><span class="token1">;</span> <span class="token6">// response里面返回的config.data是个字符串对象</span> <span class="token2">return</span> \<span class="token1">[</span>url<span class="token1">,</span> method<span class="token1">,</span> <span class="token5">JSON</span><span class="token1">.</span><span class="token3">stringify</span><span class="token1">(</span>params<span class="token1">)</span><span class="token1">,</span> <span class="token5">JSON</span><span class="token1">.</span><span class="token3">stringify</span><span class="token1">(</span>data<span class="token1">)</span>\<span class="token1">]</span><span class="token1">.</span><span class="token3">join</span><span class="token1">(</span><span class="token4">'&'</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token">/</span>\<span class="token">*</span>\<span class="token">*</span> \<span class="token">*</span> 储存每个请求唯一值<span class="token1">,</span> <span class="token3">也就是cancel</span><span class="token1">(</span><span class="token1">)</span>方法<span class="token1">,</span> 用于取消请求 \<span class="token">*</span> @param <span class="token1">{</span>\<span class="token">*</span><span class="token1">}</span> config \<span class="token">*</span><span class="token">/</span> <span class="token2">function</span> <span class="token3">addPending</span><span class="token1">(</span>config<span class="token1">)</span> <span class="token1">{</span> <span class="token2">const</span> pendingKey \<span class="token">=</span> <span class="token3">getPendingKey</span><span class="token1">(</span>config<span class="token1">)</span><span class="token1">;</span> config<span class="token1">.</span>cancelToken \<span class="token">=</span> config<span class="token1">.</span>cancelToken <span class="token">||</span> <span class="token2">new</span> <span class="token3">axios<span class="token1">.</span>CancelToken</span><span class="token1">(</span><span class="token1">(</span>cancel<span class="token1">)</span> \<span class="token">=></span> <span class="token1">{</span> <span class="token2">if</span> <span class="token1">(</span><span class="token">!</span>pendingMap<span class="token1">.</span><span class="token3">has</span><span class="token1">(</span>pendingKey<span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> pendingMap<span class="token1">.</span><span class="token2">set</span><span class="token1">(</span>pendingKey<span class="token1">,</span> cancel<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token">/</span>\<span class="token">*</span>\<span class="token">*</span> \<span class="token">*</span> 删除重复的请求 \<span class="token">*</span> @param <span class="token1">{</span>\<span class="token">*</span><span class="token1">}</span> config \<span class="token">*</span><span class="token">/</span> <span class="token2">function</span> <span class="token3">removePending</span><span class="token1">(</span>config<span class="token1">)</span> <span class="token1">{</span> <span class="token2">const</span> pendingKey \<span class="token">=</span> <span class="token3">getPendingKey</span><span class="token1">(</span>config<span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span>pendingMap<span class="token1">.</span><span class="token3">has</span><span class="token1">(</span>pendingKey<span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">const</span> cancelToken \<span class="token">=</span> pendingMap<span class="token1">.</span><span class="token2">get</span><span class="token1">(</span>pendingKey<span class="token1">)</span><span class="token1">;</span> <span class="token3">cancelToken</span><span class="token1">(</span>pendingKey<span class="token1">)</span><span class="token1">;</span> pendingMap<span class="token1">.</span><span class="token2">delete</span><span class="token1">(</span>pendingKey<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token2">export</span> <span class="token2">default</span> myAxios ``` ``` ## vue测试使用 ``` <pre class="calibre14">``` <span class="token"><</span><span class="token">!</span><span class="token">--</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span>' \<span class="token">*</span> <span class="token4">'::::::::::::'</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span> \<span class="token">*</span> '<span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span><span class="token1">.</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span><span class="token4">``</span></span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span> \<span class="token">*</span> <span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span><span class="token4">``</span></span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span>' <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token4">' '</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span>' <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token4">' :::: .:::::::'</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token4">' ::::: .:::::::::'</span> '<span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token4">' :::::.:::::::::'</span> '<span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span class="token1">.</span><span class="token1">:</span><span class="token1">:</span><span class="token4">' ::::::::::::::'</span> <span><span class="token4">``</span></span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span class="token">...</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span> <span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span>' <span><span class="token4">``</span></span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span> \<span class="token">*</span> <span><span class="token4">``</span></span><span><span class="token4">``</span></span><span class="token4">':. '</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span>' <span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">:</span><span class="token1">.</span><span class="token1">.</span> \<span class="token">*</span> <span class="token4">'.:::::'</span> <span class="token4">':'</span><span><span class="token4">``</span></span><span><span class="token4">``</span></span><span class="token1">.</span><span class="token1">.</span> \<span class="token">*</span> \<span class="token">*</span> @Author<span class="token1">:</span> 洋宝儿 \<span class="token">*</span> @Date<span class="token1">:</span> <span class="token5">2022</span><span class="token">-</span><span class="token5">02</span><span class="token">-</span><span class="token5">16</span> <span class="token5">10</span><span class="token1">:</span><span class="token5">57</span><span class="token1">:</span><span class="token5">45</span> \<span class="token">*</span> @Description<span class="token1">:</span> \<span class="token">*</span> @FilePath<span class="token1">:</span> \\ViteDemo01\\src\\view\\axiosview\\axiosview<span class="token1">.</span>vue <span class="token">--</span><span class="token">></span> axios页面触发请求 <span class="token3">axios页面触发请求</span><span class="token1">(</span>无格式<span class="token1">)</span> <span class="token2">import</span> myAxios <span class="token2">from</span> <span class="token4">"@/axiosintrnce/axiosinterce.js"</span><span class="token1">;</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>window<span class="token1">.</span>navigator<span class="token1">.</span>onLine<span class="token1">)</span><span class="token1">;</span> <span class="token">/</span>\<span class="token">*</span> json传输 使用中间件的取消函数达到即使点了多次 只有一次生效 使用配置化取消重复请求 \<span class="token">*</span><span class="token">/</span> <span class="token2">const</span> submitBtn \<span class="token">=</span> <span class="token1">(</span><span class="token1">)</span> \<span class="token">=></span> <span class="token1">{</span> <span class="token3">sendPost</span><span class="token1">(</span><span class="token1">)</span> <span class="token3">setTimeout</span><span class="token1">(</span><span class="token1">(</span><span class="token1">)</span>\<span class="token">=></span><span class="token1">{</span> <span class="token3">sendPost</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">}</span><span class="token1">,</span><span class="token5">200</span><span class="token1">)</span> <span class="token3">setTimeout</span><span class="token1">(</span><span class="token1">(</span><span class="token1">)</span>\<span class="token">=></span><span class="token1">{</span> <span class="token3">sendPost</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">}</span><span class="token1">,</span><span class="token5">300</span><span class="token1">)</span> <span class="token1">}</span><span class="token1">;</span> <span class="token2">const</span> sendPost \<span class="token">=</span> <span class="token1">(</span><span class="token1">)</span> \<span class="token">=></span> <span class="token1">{</span> <span class="token3">myAxios</span><span class="token1">(</span><span class="token1">{</span> url<span class="token1">:</span> <span class="token4">"/abc/sda"</span><span class="token1">,</span> method<span class="token1">:</span> <span class="token4">"post"</span><span class="token1">,</span> data<span class="token1">:</span> <span class="token1">{</span> id<span class="token1">:</span> <span class="token5">256</span> <span class="token1">}</span><span class="token1">,</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">.</span><span class="token3">then</span><span class="token1">(</span><span class="token1">(</span>res<span class="token1">)</span> \<span class="token">=></span> <span class="token1">{</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span><span class="token4">"result==>"</span><span class="token1">,</span> res<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">;</span> <span class="token7">/\*无格式传输\*/</span> <span class="token2">const</span> unsubmitBtn \<span class="token">=</span> <span class="token1">(</span><span class="token1">)</span> \<span class="token">=></span> <span class="token1">{</span> <span class="token3">myAxios</span><span class="token1">(</span><span class="token1">{</span> url<span class="token1">:</span> <span class="token4">"/abc/sda"</span><span class="token1">,</span> method<span class="token1">:</span> <span class="token4">"get"</span><span class="token1">,</span> data<span class="token1">:</span> <span class="token4">"id=11"</span><span class="token1">,</span> headers<span class="token1">:</span> <span class="token1">{</span> <span class="token4">"Content-Type"</span><span class="token1">:</span> <span class="token4">"application/x-www-form-urlencoded"</span><span class="token1">,</span> <span class="token1">}</span><span class="token1">,</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">.</span><span class="token3">then</span><span class="token1">(</span><span class="token1">(</span>res<span class="token1">)</span> \<span class="token">=></span> <span class="token1">{</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span><span class="token4">"result==>"</span><span class="token1">,</span> res<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">;</span> ``` ```