From ad0f9dd921ef014eb4445de258e0494ba09819fd Mon Sep 17 00:00:00 2001 From: dyh_a Date: Thu, 27 Oct 2022 12:09:19 +0800 Subject: [PATCH] =?UTF-8?q?feat(AxiosRequestTemplate):=20useAxios=E5=86=85?= =?UTF-8?q?'AxiosRequestTemplate.axios=20=3D=20axios;'=E6=94=B9=E4=B8=BA't?= =?UTF-8?q?his.axios=20=3D=20axios;'=E4=B9=9F=E5=B0=B1=E6=98=AF=E8=AF=B4?= =?UTF-8?q?=E5=AD=90=E7=B1=BB=E4=B9=9F=E5=8F=AF=E4=BB=A5=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?axios=E8=83=BD=E5=A4=9F=E8=A6=86=E7=9B=96=E7=88=B6=E7=B1=BB?= =?UTF-8?q?=E7=9A=84axios=EF=BC=8Cinit=E6=97=B6=E5=A6=82=E6=9E=9C=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E5=88=B0=E6=9C=AA=E4=BC=A0=E5=85=A5axios=E5=B0=86?= =?UTF-8?q?=E6=8A=9B=E5=87=BA=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AxiosRequestTemplate.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/AxiosRequestTemplate.ts b/src/AxiosRequestTemplate.ts index 55762b0..4d0f319 100644 --- a/src/AxiosRequestTemplate.ts +++ b/src/AxiosRequestTemplate.ts @@ -16,7 +16,7 @@ export class AxiosRequestTemplate< * 用axios作为请求工具时必须调用该方法 */ static useAxios(axios: AxiosStatic) { - AxiosRequestTemplate.axios = axios; + this.axios = axios; } /** @@ -25,8 +25,14 @@ export class AxiosRequestTemplate< protected axiosIns!: AxiosInstance; protected init() { + const proto = Object.getPrototypeOf(this).constructor; + const axios = proto.axios; + if (!axios || typeof axios.create !== 'function') { + throw new Error('使用前必须先传入axios,调用useAxios(axios)'); + } + // 1、保存基础配置 - this.axiosIns = AxiosRequestTemplate.axios.create(this.globalConfigs.requestConfig); + this.axiosIns = axios.create(this.globalConfigs.requestConfig); super.init(); this.setInterceptors(); } @@ -35,7 +41,7 @@ export class AxiosRequestTemplate< * 获取拦截器 */ protected get interceptors() { - return this.axiosIns?.interceptors; + return this.axiosIns.interceptors; } /**